Remove duplicates and original also

조회 수: 24 (최근 30일)
Declan Bourke
Declan Bourke 2016년 8월 5일
댓글: Stephen23 2016년 8월 8일
so i'm having trouble trying to eliminate repetitions from my arrays. i've been using unique but can't have the first instance of the element in the return. for example:
A = 1,2,2,2,3,3,3,4,5; B = unique(A); B = 1,2,3,4,5
what i'm actually looking for is B = 1,4,5 where even the first instance of repeating elements is taken out. is this possible?
Thanks
  댓글 수: 2
Declan Bourke
Declan Bourke 2016년 8월 8일
Hi,
And could i ask quickly, is there any way to do the same with an array of strings? e.g. (AA,AB,AC,AA,AD). I've tried everything I can think of.
Thanks,
Declan

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 8월 5일
편집: Stephen23 2016년 8월 8일
Assuming that the vector A is sorted:
>> A = [1,2,2,2,3,3,3,4,5];
>> B = unique(A);
>> B(histc(A,B)<2)
ans =
1 4 5
If A is not sorted, then this is a bit more complicated:
>> A = [3,3,3,5,1,4,2,2,2,2];
>> [B,X] = unique(A);
>> Y = histc(A,B)<2;
>> A(sort(X(Y))) % in original order
ans =
5 1 4
>> A(X(Y)) % sorted
ans =
1 4 5

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 5일
A = [1,2,2,2,3,3,3,4,5]
b=unique(A,'stable')
c=hist(A,b)
B=b(c==1)
  댓글 수: 2
Declan Bourke
Declan Bourke 2016년 8월 5일
Thanks for the help!
Stephen23
Stephen23 2016년 8월 5일
편집: Stephen23 2016년 8월 5일
This does not work if A is not sorted into order:
>> A = [3,3,3,5,1,4,2,2,2,2];
>> b=unique(A,'stable');
>> c=hist(A,b)
Error using histc
Edge vector must be monotonically non-decreasing.
Error in hist (line 92)
nn = histc(y,edges,1);
Although beginners might think that the 'stable' makes a difference, this then provides a non-monotonic sequence to histc, which is not allowed.

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 5일
편집: Azzi Abdelmalek 2016년 8월 5일
[ii,jj,kk]=unique(A)
out=ii(accumarray(kk,1)==1)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by