# How to remove repeating elments from a matrix?

1 view (last 30 days)
Pushkar Satish Marathe on 3 Nov 2017
Commented: Cedric Wannaz on 3 Nov 2017
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 Wannaz on 3 Nov 2017
Edited: Cedric Wannaz on 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 CommentsShowHide 1 older comment
Cedric Wannaz on 3 Nov 2017
My pleasure!

### Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!