How to compare a matrix row by row with specified condition

2 views (last 30 days)
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

Accepted Answer

José-Luis
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);
  1 Comment
Lei
Lei on 30 Oct 2012
Hello,José-Luis. Thanks for your help! for my example, the output would be something like this: ind1=[ 1,3],ind2=[2,4] because the combination of row 1 and row 3, and the combination of row 2 and row 4 meet my specified condition. When I compare the two rows,I need the two rows meet both conditions: the 1st and 2nd column of this row is the same as the 3rd and 4th columns of the second row, and the 3rd and 4th columns of the this row is the same as the 1st and 2nd column of the other row

Sign in to comment.

More Answers (0)

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!