MATLAB: Produce equality matrix based on elements in vector.

MATLABmatrixvector

Given two equally sized vectors A and B, is there any way to make a matrix C of 1's and zero's such that the kth row of C contains 1's wherever elements of B equal the kth element of A?
I can do it by looping through elements of A, but I want to know if there's a vectorised way of doing this to speed it up?

Best Answer

Trivially done.
%assuming A and B are both row vectors:
C = A.' == B;
If they're both column vectors, transpose B instead.
Related Question