This is what is described in one of the examples for ismember:
Define two vectors with values in common.
A = [5 3 4 2]; B = [2 4 4
A simple approach is to use bsxfun
to test for equality between each element of A
and B
:
ind = bsxfun(@eq, A(:), B(:).');
list = cellfun(@find, mat2cell(ind, ones(1,numel(A)), numel(B)), 'uni', 0);
The matrix ind
gives the result in logical form (i.e. 0 or 1 values), and list
is a cell array containing the indices:
>> ind
ind =
0 0 0 0 0 0
0 0 0 0 0 0
0 1 1 1 0 0
1 0 0 0 0 0
>> celldisp(list)
list{1} =
[]
list{2} =
[]
list{3} =
2 3 4
list{4} =
1