Same Rows between two matrix

조회 수: 6 (최근 30일)
Sayid Bajrai Nasir
Sayid Bajrai Nasir 2020년 3월 24일
댓글: Sayid Bajrai Nasir 2020년 3월 24일
How do I find matricies with the same rows? I need to find unque Matricies.
A = [1 2; 3 4; 6 7], B = [3 4; 1 2; 6 7], C = [7 8; 8 2; 1 3], D = [1 2; 1 2; 6 7]
  • A is considered the same as B, since A(1,:) = B(2,:), A(2,:) = B(1,:) and A(3,:) = B(3,:).
  • A and B is not the same as C, since the rows do not match.
  • A, B is not considered the same as D, even when D(1,:) = A(1,:) but also D(2,:) = A(1,:).
  • Each rows (in matrix X) should correspond to the other row (in matrix Y).
  • all matrix have the same size.
Using the above rules, I only need unique matrix, so output should be just A, C and D. (I dont need index, but ok with it outputing index)
  댓글 수: 1
Sayid Bajrai Nasir
Sayid Bajrai Nasir 2020년 3월 24일
Just to be clear, elements in each row can be sorted:
C = [7 8; 8 2; 1 3] => C = [7 8; 2 8; 1 3] these are considered the same.

댓글을 달려면 로그인하십시오.

채택된 답변

David Hill
David Hill 2020년 3월 24일
isequal(sortrows(A),sortrows(B));
  댓글 수: 2
David Hill
David Hill 2020년 3월 24일
isequal(sortrows(sort(A,2)),sortrows(sort(B,2));
Sayid Bajrai Nasir
Sayid Bajrai Nasir 2020년 3월 24일
Thanks it seems to work for now.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

per isakson
per isakson 2020년 3월 24일
편집: per isakson 2020년 3월 24일
>> setdiff( A, B, 'rows' )
ans =
0×2 empty double matrix
>> setdiff( A, C, 'rows' )
ans =
1 2
3 4
"Same Rows between two matrix" see union, Set union of two arrays
  댓글 수: 1
Sayid Bajrai Nasir
Sayid Bajrai Nasir 2020년 3월 24일
This may have been my fault for not using a clearer example, If we consider
X = [1 2; 3 4; 1 2; 3 4], Y = [1 2; 1 2; 1 2; 3 4];
setdiff( X, Y, 'rows' )
ans =
0×2 empty double matrix
But X and Y are not considered the same, and the output would be both X and Y, not just X

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by