How To Remove Repeating Pattern From Vector

2 次查看(过去 30 天)
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

采纳的回答

John D'Errico
John D'Errico 2019-5-1
编辑:John D'Errico 2019-5-1
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 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by