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

조회 수: 5 (최근 30일)
Shweta Saboo
Shweta Saboo 2021년 10월 7일
댓글: Walter Roberson 2021년 10월 7일
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.

답변 (1개)

Walter Roberson
Walter Roberson 2021년 10월 7일
편집: Walter Roberson 2021년 10월 7일
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
Shweta Saboo
Shweta Saboo 2021년 10월 7일
Thank you so much for the quick answer. But I want to plot each segment into different figure.
Walter Roberson
Walter Roberson 2021년 10월 7일
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by