How to remove repeating elments from a matrix?

조회 수: 1 (최근 30일)
Pushkar Satish Marathe
Pushkar Satish Marathe 2017년 11월 3일
댓글: Cedric 2017년 11월 3일
I have following matrix.
A =
1 2 2 1 3
2 1 3 1 1
Where, first two columns are in principle are unique combinations. I would like to remove one column, either 1st or 2nd, and generate following matrix
B =
2 2 1 3
1 3 1 1
OR
B =
1 2 1 3
2 3 1 1
I tried using following
[G,ia,ic] = unique(A(:,1:2),'rows');
uA = A(ia,:)
but it doesn't solve the problem and gives following solution
uA =
1 2 2 1 3
2 1 3 1 1

채택된 답변

Cedric
Cedric 2017년 11월 3일
편집: Cedric 2017년 11월 3일
Almost, the idea was correct but you were not working on the correct dimension. Also, sorting the input array vertically makes columns composed of the same elements identical regardless of their order:
>> [~, ia] = unique( sort(A, 1).', 'rows', 'stable' ) ;
>> B = A(:, ia)
B =
1 2 1 3
2 3 1 1
  댓글 수: 2
Pushkar Satish Marathe
Pushkar Satish Marathe 2017년 11월 3일
Worked very well !! Thanks a lot :)
Cedric
Cedric 2017년 11월 3일
My pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by