How to remove repeating elements but maintain occurrences in an array?
Afficher commentaires plus anciens
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 commentaires
Why do this in a loop when you can use the vectorized solution that I posted? Loops are a poor way to solve this problem in MATLAB, vectorization is much neater and faster .
Image Analyst
le 12 Fév 2015
I would not use ans as the name of a variable - that has a special meeting in MATLAB.
Vignesh
le 15 Fév 2015
Réponse acceptée
Plus de réponses (1)
ans=unique(X);
2 commentaires
Walter Roberson
le 19 Juil 2024
- This has the side effect of sorting the output, which was not desired
- 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.
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)
% sorted output from unique()
usort = unique(X)
% stable output from unique()
usort = unique(X,'stable')
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.
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!