How to remove duplicate element from matrix ?

1 view (last 30 days)
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);

Accepted Answer

Voss
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
  3 Comments
Arshub
Arshub on 31 Dec 2021
Edited: Arshub on 31 Dec 2021
@Benjamin can you help me in this state:
I need to use C=[1 3 4 5 6 7 9 11 13 14 15 1 7 9 11 11]
as index to perform permutation of matrix A =[111 222 30 4;50 65 70 83; 10 27 39 40 ; 54 67 72 81 ] by this method:
Substitute A(Ci ) with A (CM×N−i+1).
here i = 1, 2,..., M × N/2.
then i need to make reverse operartion to get the original matrix.
I write the code but when I make reverse operation I coulden't get the original matrix "M" after permutation, Why?
What should I do to get original matrix from permutated matrix?
Note: it is immportant to me use "Substitute A(Ci ) with A (CM×N−i+1)."
to permutate matrix.
%-----------------------------------------------------my rest of code ---------------------------------
A =[111 222 30 4;50 65 70 83; 10 27 39 40 ; 54 67 72 81 ];
[row,col]=size(A);
len=row*col;
A=reshape(A,1,len);
for i =1:len/2
A(C(i))=A(C(len-i+1));% Substitute A(Ci ) with A (CM×N−i+1)., permutation matrix
end
Ab=reshape(A,row,col)
AA=reshape(Ab,1,len);
for i =1:len/2
AA(C(len-i+1))=AA(C(i));% reverse Substitute A (CM×N−i+1) with A(Ci ) to get original matrix
end
AA=reshape(AA,row,col)
Voss
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))];

Sign in to comment.

More Answers (0)

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!