MATLAB: Ismember function — return all indexes, not just lowest

ismember

According to the Matlab documentation,
[Lia,Locb] = ismember(A,B) returns an array, Locb, containing the lowest index in B for each value in A that is a member of B.
Is there a way to return an array (or matrix) containing all indexes in B for each value in A that is a member of B?

Best Answer

  • For numeric A and B, I would probably do something like this
    Lia = ismember(A,B)
    idx=find(Lia);
    map=bsxfun(@eq, A(idx),B(:));
    Then map(:,i) will be a logical index into B of all points where B equals A(idx(i))