Comparing arrays between matrices in random order

2 views (last 30 days)
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 Comment
Cedric
Cedric on 29 Jul 2013
Edited: Cedric on 29 Jul 2013
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).

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 29 Jul 2013
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{:}

More Answers (1)

Roger Stafford
Roger Stafford on 29 Jul 2013
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.)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!