MATLAB: Rapid Vector Matching/Search Problem

computational speedpatternsearch

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

Best Answer

  • you'll need to use the function ismember()
    such as:
    found_row = ismember(A,B,'rows')
    which will return a logical array where a 1 will represent a match. then by doing something like this
    A= randi(2,100,4);
    indexIntrest=find(ismember(A,[ 2 1 1 1],'rows'))
    will return the indexes that match.