I want to plot non-zero consecutive segments from an array
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
Walter Roberson
2021 年 10 月 7 日
編集済み: Walter Roberson
2021 年 10 月 7 日
A = [0 0 0 1 2 3 0 0 0 0 8 9 6]
starts = strfind([false, logical(A)], [0 1])
stops = strfind([logical(A), false], [1 0])
hold on
arrayfun(@(B,E) plot(B:E, A(B:E), '-*'), starts, stops);
hold off
xlim auto; ylim auto
2 件のコメント
Walter Roberson
2021 年 10 月 7 日
A = [0 0 0 1 2 3 0 0 0 0 8 9 6]
starts = strfind([false, logical(A)], [0 1])
stops = strfind([logical(A), false], [1 0])
arrayfun(@(B,E) plot(gca(figure()), B:E, A(B:E), '-*'), starts, stops);
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!