Comparing arrays between matrices in random order
Mostrar comentarios más antiguos
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 comentario
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).
Respuesta aceptada
Más respuestas (1)
Roger Stafford
el 29 de Jul. de 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.)
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!