Rapid Vector Matching/Search Problem

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 件のコメント

Oleg Komarov
Oleg Komarov 2014 年 8 月 23 日
Have you tried the bsxfun() solution?

サインインしてコメントする。

 採用された回答

Joseph Cheng
Joseph Cheng 2014 年 8 月 20 日
編集済み: Joseph Cheng 2014 年 8 月 20 日

1 投票

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.

3 件のコメント

Kyle Johnston
Kyle Johnston 2014 年 8 月 20 日
I am familiar with "ismember" and I was hoping for something faster then that. I have also tried implementing fitctree, assigning each row a "class" and then using the resulting tree to quickly find the location of the matching row. But that is also too slow.
if it helps, A is resulting from the function perms(1:n) in MATLAB, and B is a member of the set that A represents.
I've settled on the following for right now:
space = factorial(n)/n;
for i = (space*(n- B(1)) + 1) : space*(n- B(1) + 1)
if (isequal(A(i,:),B))
indexIntrest = i
break;
end
end
And by the way, the example with ismember will yield:
[found_row, indexInterest] = ismember(A,B,'rows')
making the find given in the answer above not necessary
Joseph Cheng
Joseph Cheng 2014 年 8 月 21 日
I did it that way just in case you had more than 1. When i do your ismember(A,B,'rows') it does not come back with the index of which row.. that is why i used find to come up with the index.
Joseph Cheng
Joseph Cheng 2014 年 8 月 21 日
Alright finally grasped how the perms() function lays out the permutations. Needed to eat a cookie and it came to me.
clc
clear all
n=10;
A= perms(1:n);
B=A(randi(length(A),1),:);
space = factorial(n-1);
tic
for i = (space*(n- B(1)) + 1) : space*(n- B(1) + 1)
if (isequal(A(i,:),B))
index3 = i
break;
end
end
looptime = toc;
index4 = 1;
tic
seq = [n:-1:1];
for i =1:n-1
space=factorial(n-i);
Subind = find(seq==B(i)); %determine which section
index4= index4+space*(Subind-1);
seq = A(index4,i+1:end); %contains order of next column
end
calctime = toc;
disp([A(index4,:);B])
disp(['loop time: ' num2str(looptime)])
disp(['calc time: ' num2str(calctime)])
disp(['loop - calc: ' num2str(looptime-calctime)])

サインインしてコメントする。

その他の回答 (1 件)

Oleg Komarov
Oleg Komarov 2014 年 8 月 21 日

2 投票

You can try to use bsxfun():
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.

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by