Find and edit interval of array when element is equal to a value?
Show older comments
Hi everyone!
I have a small question.
I have an array that looks something like this [0 0 2 2 2 2 2 0 0 0 2 2 2 2 2 2 2 0 0 0 0 2 2]
And I'd like to find those intervals of consectuve 2s that are longer than 3 elements. After that I find those interval, I want to cut the first elements untill they reach the length for 3 elements.
So my final array should be like this [0 0 0 0 2 2 2 0 0 0 0 0 0 0 2 2 2 0 0 0 0 2 2]
The first interval of 2s was made by 5 elements, now it's only 3 elements.
The second interval of 2s was made by 7 elements, now it's only 3.
The last one was made by 2 elements, it says the same.
Is it possible to do this?
Thank you very much and I hope you're having a nice day
Accepted Answer
More Answers (1)
Andrei Bobrov
on 8 Jul 2019
Edited: Andrei Bobrov
on 9 Jul 2019
i1 = double(diff([A,0]) == -2);
ii = find(i1) - 3;
i1(ii(ii > 0)) = -1;
out = cumsum(i1,'revers').*A
Categories
Find more on MATLAB Mobile Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!