Reputation: 181
looking for an efficient way to vectorize a search
say A is 2 by 4 array
x y z a
.2 0.97 34.2 4.5
and B is a 1 by 1000 array
a x x x x y y y z z a .........
How do I get the corresponding row 2 values of A for array B. Looking for a vectorized efficient solution, I have an if loop that works but not efficient.
Thanks
I currently have
A(2,A(1,:)==B(:))
but this doesn't work as both arrays have different sizes. Thanks
Upvotes: 0
Views: 69
Reputation: 12345
You can use the second output from the ismember
function, like this:
%Setup
A = [24 25 26 1; 0.2 0.97 34.2 4.5]
B = [1 24 24 24 24 25 25 25 26 26 1];
%Use ismember to get matching indexes
[~, ixs] = ismember(B, A(1,:))
%Use indexes to get desired result
out = A(2,ixs)
Upvotes: 1