MATLAB Answers

How to remove repeating elments from a matrix?

조회 수: 3(최근 30일)
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

  댓글 수: 0

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

채택된 답변

Cedric Wannaz
Cedric Wannaz 3 Nov 2017
편집: Cedric Wannaz 3 Nov 2017
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
Worked very well !! Thanks a lot :)
Cedric Wannaz
Cedric Wannaz 3 Nov 2017
My pleasure!

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by