How to remove duplicate element from matrix ?

조회 수: 3 (최근 30일)
Arshub
Arshub 2021년 12월 30일
댓글: Voss 2022년 1월 1일
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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by