octave/matlab: create new matrix based on existence of words from one matrix in another -


in octave/matlab, have:

all = {   [1,1] = 1   [1,2] = 2   [1,3] = 3   [1,4] = 4   [1,5] = 5   [1,6] = 6   [1,7] = 7   [1,8] = 8   [1,9] = 9   [1,10] = ten }  = {   [1,1] = someword   [1,2] = someword   [1,3] = 1   [1,4] = someword   [1,5] = 9 } 

how make new matrix such that

new = {   [1,1] = 1   [1,2] = 0   [1,3] = 0   [1,4] = 0   [1,5] = 0   [1,6] = 0   [1,7] = 0   [1,8] = 0   [1,9] = 1   [1,10] = 0 } 

that is, new matrix has same size all matrix, values either 1 or 0, depending on whether words in some exist in all?

easily for-loop:

new = cell(size(all)); v=1:length(all)     if any(strcmp(some,all{v}))         new{v}=1;     else         new{v}=0;     end end 

alternatively, use intersect:

[isect, index_all, index_some]=intersect(all,some); 

if don't need new values cell (for 0 or one, there's no reason not use simple array), easy:

new=zeros(size(all)); new(index_all)=1; 

if need them cell reason, use:

new=num2cell(new); 

ps: should't use all variable name - inbuilt matlab function, , overwriting variable, cannot use until clear variable.


Comments