compare each row of a vector to the rows of another vector and save as logical in matlab

To compare each row of a vector to rows of another vector, you can use the bsxfun function in MATLAB. Assuming that you have two row vectors A and B, you can use the following code to get a logical matrix C where C(i,j) is true if and only if the ith row of A is equal to the jth row of B:

main.m
C = bsxfun(@eq, A.', B);
25 chars
2 lines

Here, A.' is used to transpose the rows of A to columns so that it can be broadcasted with B. The bsxfun function applies the eq function element-wise to the two input matrices. The resulting matrix C will have size num_rows_A x num_rows_B.

Note that bsxfun is very efficient since it avoids the creation of unnecessary temporary arrays.

I hope this helps!

gistlibby LogSnag