How To Remove Repeating Pattern From Vector

2 views (last 30 days)
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

Accepted Answer

John D'Errico
John D'Errico on 1 May 2019
Edited: John D'Errico on 1 May 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]);

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!