1
votes

suppose I have values need_find = [1 3 4] and a matrix A in the size of 3xK. I want to find the values of need_find on its corresponding row of A. How can I apply vectorized solution in matlab instead of iterate over each row?

For detailed example as expected;

A = [1 3 4; 1 3 5; 3 4 5];
my_method_do_what_I_want(A,need_find);

The method returns

ans = [1;2;2] 
% so I find the index of each element of need_find at corresponding row at A

Long story short :seach 1 at row 1, search 3 at row2, search 4 at row3

1
Not clear, what is "its corresponding row in A"? Please, provide an example. Or did you mean A is K by 3.Oleg
A is 3xK matrix. 3 rows, K columnserogol
You mean the index of the first element in each row that matches one of the elements in need_find?horchler
@horchler not exactly, seach 1 at row 1, search 3 at row2, search 4 at row3erogol

1 Answers

1
votes

Here's one way:

A = [1 3 4; 1 3 5; 3 4 5];
need_find = [1 3 4]
[~,idx] = find(bsxfun(@eq,A,need_find(:)))

which returns

idx =

     1
     2
     2

This simple one-liner won't work if you have repeated values in the rows of A or if there are no matches at all, but I can only go by your example...