Given a vector such as a = [2 5 9]
and a matrix such as
8 11 5
b = 2 6 1
4 9 3
What's the best way to find which column of b
contains each element of a
? In this example I'd want an output like [1 3 2]
because 2
is in the first column, 5
is in the third column, and 9
is in the second column. For my purposes it's safe to assume that a number can only appear in one column.
One approach -
[colID,~] = find(squeeze(any(bsxfun(@eq,b,permute(a,[1 3 2])),1)))
Or if you would like to avoid squeeze
and any
-
[~,colID,~] = ind2sub([size(b) numel(a)],find(bsxfun(@eq,b(:),a)))