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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by