Rapid Vector Matching/Search Problem
Show older comments
I have a matrix (we'll call A) of size m x n, and a vector (we'll call B) that is a 1 x n, and I am interested in finding the one unique index (and I know there is just one) where A(index,:) equals B, is there a way for me to quickly make the determination in MATLAB besides using the following code:
for i = 1 : m
if ((isequal(A(i,:), B)))
indexIntrest = i;
break;
end
end
Thanks
1 Comment
Oleg Komarov
on 23 Aug 2014
Have you tried the bsxfun() solution?
Accepted Answer
More Answers (1)
Oleg Komarov
on 21 Aug 2014
find(all(bsxfun(@eq, A,B),2))
What happens is every row of A is tested with @eq for element-wise equality against B, producing a m x n logical matrix. Then, all() tests each row if all elements are true, and find() returns the position.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!