How To Remove Repeating Pattern From Vector
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Matthew
le 1 Mai 2019
Modifié(e) : John D'Errico
le 1 Mai 2019
Suppose I have a vector that looks like this:
x=[1;1;1;1;2;2;2;2;3;3;3;3;4;4;4;4;5;5;5;5;6;6;6;6;7;7;7;7;8;8;8;8;9;9;9;9]
You can see the repeating pattern of four-by-four. Is there a command that can shave this down to something like this?...
xx=[1;2;3;4;5;6;7;8;9]
I checked "decimate" but I don't think it does what I want. Yes, I could construct an IF statement, but I was hoping there was a "one-liner" that might do it quickly and easily.
Thanks in advance!
M Ridzon
0 commentaires
Réponse acceptée
John D'Errico
le 1 Mai 2019
Modifié(e) : John D'Errico
le 1 Mai 2019
x(find(diff(x) == 0) + 1) = [];
That just drops out the consecutive reps, changing x.
If you want to create a new vector, you might have done this:
xx = x([true;diff(x)~=0]);
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!