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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!