필터 지우기
필터 지우기

How to remove repeating elements but maintain occurrences in an array?

조회 수: 16 (최근 30일)
Vignesh
Vignesh 2015년 2월 12일
편집: DGM 2024년 7월 19일
Is there a way that I can get the first occurrence of consecutively repeating values, even if the same value occurs at few different places in the matrix?
Say I have a matrix
X=[2 2 2 2 -1 -1 -1 6 6 6 6 6 5 5 5 2 2 2 2 7 7 6 6]
I want the result to be
ans=[2 -1 6 5 2 7 6].
I've checked a similar question How to remove repeating elements from an array but using unique and keeping the same order as original matrix gives
ans=[2 -1 6 5 7].
I know I can use a loop like below. But I was wondering if there's a function for this.
for i=1:(length(X)-1)
if (X(i+1,6)-X(i,6))~=0
ans(i,1)=X(i+1,6);
else
ans(i,1)=NaN;
end
end
A function to do this will be great.
  댓글 수: 3
Image Analyst
Image Analyst 2015년 2월 12일
I would not use ans as the name of a variable - that has a special meeting in MATLAB.
Vignesh
Vignesh 2015년 2월 15일
Thanks Stephen! That helped. Image Analyst, it was just an example for the question. Thanks for pointing out anyway.

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

채택된 답변

Stephen23
Stephen23 2015년 2월 12일
편집: Stephen23 2015년 2월 12일
You could do this with diff:
>> X=[2 2 2 2 -1 -1 -1 6 6 6 6 6 5 5 5 2 2 2 2 7 7 6 6];
>> Y = [true,diff(X)~=0];
>> X(Y)
ans = [2,-1,6,5,2,7,6]
This works well with integers. If you are working with floating point numbers, then you might need to include some kind of tolerance in the diff:
>> Y = [true,abs(diff(X))>tol];
  댓글 수: 1
Stephen23
Stephen23 2015년 2월 12일
편집: Stephen23 2015년 2월 12일
If you want this as a function, then you can easily define your own:
>> start = @(v)v([true,diff(v)~=0]);
>> start(X)
ans = 2 -1 6 5 2 7 6

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

추가 답변 (1개)

Prashanth
Prashanth 2024년 7월 19일
편집: Prashanth 2024년 7월 19일
ans=unique(X);
  댓글 수: 2
Walter Roberson
Walter Roberson 2024년 7월 19일
  1. This has the side effect of sorting the output, which was not desired
  2. This as the side effect of only outputing any one value once. We see from the example input and output that 2 is expected as the output twice.
DGM
DGM 2024년 7월 19일
편집: DGM 2024년 7월 19일
For example:
% our input vector
X = [2 2 2 2 -1 -1 -1 6 6 6 6 6 5 5 5 2 2 2 2 7 7 6 6];
% Stephen's answer
start = @(v)v([true,diff(v)~=0]);
start(X)
ans = 1x7
2 -1 6 5 2 7 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% sorted output from unique()
usort = unique(X)
usort = 1x5
-1 2 5 6 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% stable output from unique()
usort = unique(X,'stable')
usort = 1x5
2 -1 6 5 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
While the ordering can be resolved by using the 'stable' option, we're not actually after unique values at all. The goal is to reduce each run of a given value to a single instance of that value. Since there are multiple runs of 2 and 6, the output from unique(...,'stable') still results in a loss of information.
As an aside, using 'ans' as a variable name is a problem waiting to happen.

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

카테고리

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