# How to remove duplicate element from matrix ?

5 views (last 30 days)
Arshub on 30 Dec 2021
Commented: Voss on 1 Jan 2022
I have duplicate matrix S, I need remove the repeated elements from S, and
then put the absent numbers at the end to generate a new matrix X. iI implement remove duplicate element but how we add absent element at the end?
S=[1 11 1 4 3 14 6 11 13 11 7 15 5 9 9 7];
C=unique(S);
##### 1 CommentShowHide None
Image Analyst on 30 Dec 2021
That's not a matrix. That's a vector.

Voss on 30 Dec 2021
Edited: Voss on 1 Jan 2022
Here's one way:
S=[1 11 1 4 3 14 6 11 13 11 7 15 5 9 9 7]
S = 1×16
1 11 1 4 3 14 6 11 13 11 7 15 5 9 9 7
[C,i] = unique(S,'stable')
C = 1×11
1 11 4 3 14 6 13 7 15 5 9
i = 11×1
1 2 4 5 6 7 9 11 12 13
C = [C S(~ismember(1:numel(S),i))]
C = 1×16
1 11 4 3 14 6 13 7 15 5 9 1 11 11 9 7
Voss on 1 Jan 2022
@Arshub I modifed my answer after seeing DGM's comment on your other question. I believe this answer is more what this question is looking for.
I recommend you update that other question to clarify the relationship between S and C, specifically that:
S=[1 11 1 4 3 14 6 11 13 11 7 15 5 9 9 7];
[C,i] = unique(S,'stable');
C = [C S(~ismember(1:numel(S),i))];

R2014a

### Community Treasure Hunt

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

Start Hunting!