I want to plot non-zero consecutive segments from an array

2 views (last 30 days)
Shweta Saboo
Shweta Saboo on 7 Oct 2021
Commented: Walter Roberson on 7 Oct 2021
I have a data which contains zero and non-zero values in segments like [0 0 0 1 2 3 0 0 0 0 8 9 6]. I want to plot the non-zero values which are in sequence like [1 2 3] and [8 9 6] separately.

Answers (1)

Walter Roberson
Walter Roberson on 7 Oct 2021
Edited: Walter Roberson on 7 Oct 2021
A = [0 0 0 1 2 3 0 0 0 0 8 9 6]
A = 1×13
0 0 0 1 2 3 0 0 0 0 8 9 6
starts = strfind([false, logical(A)], [0 1])
starts = 1×2
4 11
stops = strfind([logical(A), false], [1 0])
stops = 1×2
6 13
hold on
arrayfun(@(B,E) plot(B:E, A(B:E), '-*'), starts, stops);
hold off
xlim auto; ylim auto
  2 Comments
Walter Roberson
Walter Roberson on 7 Oct 2021
A = [0 0 0 1 2 3 0 0 0 0 8 9 6]
A = 1×13
0 0 0 1 2 3 0 0 0 0 8 9 6
starts = strfind([false, logical(A)], [0 1])
starts = 1×2
4 11
stops = strfind([logical(A), false], [1 0])
stops = 1×2
6 13
arrayfun(@(B,E) plot(gca(figure()), B:E, A(B:E), '-*'), starts, stops);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by