How to remove duplicate element from matrix ?

7 次查看(过去 30 天)
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);

采纳的回答

Voss
Voss 2021-12-30
编辑:Voss 2022-1-1
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 个评论
Arshub
Arshub 2021-12-31
编辑:Arshub 2021-12-31
@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 2022-1-1
@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))];

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by