Find the longest sequence of consecutive non-zero values in a vector
Show older comments
Hello everyone, I want to know how to find in a vector the longest sequence of consecutive non-zero values. example: I have x = [0 1 2 3 0 5 6 7 8 0 10]; I must have as output the block y = [5 6 7 8] Thank you in advance.
Accepted Answer
More Answers (1)
Adam
on 7 Jun 2018
0 votes
or other similar run length functions should help, especially if you trivially binarise your input first.
1 Comment
Jan
on 7 Jun 2018
x = [0 1 2 3 0 5 6 7 8 0 10];
[b, n, xIdx] = RunLength(x ~= 0);
xIdx = xIdx(b);
[maxN, nIdx] = max(n(b));
r = x(xIdx(nIdx):xIdx(nIdx) + maxN - 1)
I think, Walter's solution is nicer.
Categories
Find more on Signal Processing Toolbox 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!