How to compare a matrix row by row with specified condition
1 view (last 30 days)
Show older comments
Hello, everyone. I have a data matrix like this
A=[ 5 0 10 15 0.021
5 0 15 20 0.011
10 15 5 0 0.022
15 20 5 0 0.009]
I need to compare each row with all of the other rows. The criteria is that if the 1st and 2nd column of this row is the same as the 3rd and 4th columns of the second row, and if the 3rd and 4th columns of the this row is the same as the 1st and 2nd column of the other row, I need the indices of these two rows.For example,the 1st and 2nd columns of 1st row is 5 and 0,which is the same as the 3rd and 4th columns of third row. And the 3rd and 4th columns of 1st row is 10 and 15,which is the same as the 1st and 2nd columns of the 3rd row. I do not want change the order of my matrix. If anyone knows how can i achieve this, please let me know. thanks a lot
0 Comments
Accepted Answer
José-Luis
on 30 Oct 2012
Edited: José-Luis
on 30 Oct 2012
Maybe not the most efficient thing around, but here goes:
A=[ 5 0 10 15 0.021
5 0 15 20 0.011
10 15 5 0 0.022
15 20 5 0 0.009]
m = size(A,1);
find34 = arrayfun(@(x) {find(ismember(A(:,3:4),A(x,1:2),'rows'))},1:m,'uniformoutput',false);
find12 = arrayfun(@(x) {find(ismember(A(:,1:2),A(x,3:4),'rows'))},1:m,'uniformoutput',false);
More Answers (0)
See Also
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!