How To Remove Repeating Pattern From Vector

2 vues (au cours des 30 derniers jours)
Matthew
Matthew 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

Réponse acceptée

John D'Errico
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]);

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by