I would like to compare rows across two unequal matrices in Matlab and extract these rows to be stored in a different matrix (say D). For example,
tmp = [2245; 2345; 2290; 4576]
and
id=[1 2245 564 8890 123;
2 2445 5673 7846 342;
3 2290 3428 3321 908].
Id is a much larger matrix. I want to locate each value of tmp which is in ‘id’. Although using the intersect command in the line below I have been able to locate the rows of id which contain the values from tmp, I would like to do this for each value of tmp one by one as each value of tmp is repeated multiple times in id. I tried using foreach. However, I get an error message stating that foreach cannot be used for char type array. Could anyone please suggest an alternative how to go about this?
for j=1:length(tmp);
[D,itmp,id2] = intersect(tmp(j,1),id(:,2), 'rows');
Despite using the loop, the code doesn’t seem to take one value of j at a time. This was the reason behind trying ‘foreach j’. Also after finding the rows common to the two matrices and storing them in D, I would like to append matrix id to include the value of j next to the relevant row within id. For example, if the first value within tmp was repeated in id in rows 1,3,5,10; I would like a column in id which would take the value 1 next to rows 1,3,5,10. Any help on this would be much appreciated! Thanks.