Comparing arrays between matrices in random order

Hello all,
So, I need to determine the number of arrays (horizontally) that are part of two very large matrices that are not in the same order (see below). For example:
A = [1 2 3 4 5;2 5 3 4 6;1 4 6 3 7;1 4 2 6 4;];
B = [2 5 3 4 6;1 4 6 3 7;1 2 3 4 5;9 9 9 9 9;];
As you can see three of the arrays (horizontal) are the same for both, but one is not, and (more importantly) the order for them is not the same, so a simple comparison command does not seem to be viable. Currently I can do this calculation with two For loops, in other words; comparing each line of matrix A to every line in matrix B, for every line in matrix A. Unfortunately while this works for small matrices as the ones above, for the ones I am working with which are 100,000 plus in length (and 25 in width) it does not work. Is there a simpler/cleaner and, more importantly, faster way of calculating this?

1 件のコメント

Cedric
Cedric 2013 年 7 月 29 日
編集済み: Cedric 2013 年 7 月 29 日
Can you have multiple rows with the same numbers in different orders, and if so, should they all be counted when there is a match?
My first move, almost whichever solution is implemented, would be to sort arrays A and B (i.e. As=sort(A, 2); and same for B).

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

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 29 日

0 投票

A = [1 2 3 4 5;2 5 3 4 6;1 4 6 3 7;1 4 2 6 4];
B = [2 5 3 4 6;1 4 6 3 7;1 2 3 4 5;9 9 9 9 9];
for k=1:size(A,1)
idx{k}=find(ismember(B,A(k,:),'rows'));
end
idx{:}

その他の回答 (1 件)

Roger Stafford
Roger Stafford 2013 年 7 月 29 日

1 投票

The following assumes that any row occurring in both A and B and multiple times in either one of them will nevertheless be counted only once.
[s,ix] = sortrows([A;B]);
f = find([true;any(diff(s,1,1)~=0,2);true]);
m = size(A,1);
N = sum(ix(f(1:end-1))<=m&ix(f(2:end)-1)>m);
N is the total count I believe you are asking for.
The sort operation should decrease the total time used with large arrays since it will be of an order n*log(n) operations rather than order n^2 for n total rows.
(Note: I haven't tested this thoroughly, so let me know if it doesn't work properly.)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by