Matching two matrices by row but allowing for permutations
Show older comments
I'm having some questions about a simple strategy to rearrange two matrices such that their rows match up. Normally I would not ask immediately but I tried searching without avail, unless I haven't gone far enough.
I'm basically trying to match two matrices row-by-row, but the twist is that for the matrix to be rearranged, we only know that for each row in the target matrix, there exists a row in the candidate matrix that shares all elements. For example, we can have this:
A = [16 2;
3 13;
5 11;
10 8;
9 7;
6 12;
4 14;
15 1;]
B = [1 15;
7 9;
4 14;
16 2;
13 3;
5 11;
10 8;
12 6;]
What operation need we impose on B to match A? It is clear that there are matching rows, but not only are the rows themselves out of order, the elements within each row are also permuted.
Answers (1)
the elements within each row are also permuted.
Well, they don't have to stay that way, do they?
A=sort(A,2)
B=sort(B,2)
Now you can find corresponding row indices using ismember,
[~,loc]=ismember(B,A,'rows')
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!