How to compare a matrix row by row with specified condition

1 次查看(过去 30 天)
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

采纳的回答

José-Luis
José-Luis 2012-10-30
编辑:José-Luis 2012-10-30
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 个评论
Lei
Lei 2012-10-30
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by