How to count the number of times that values changes?
29 ビュー (過去 30 日間)
古いコメントを表示
A=[1 1 1 2 1 3 3 1 1]' I want to get:
B=[NaN 0 0 1 2 3 3 4 4]'
The second and third values of B are 0 because A doesn't change value. Fourth value is 1 because A changes value. Fifth value is 2 because A changes value again. And so on...
0 件のコメント
採用された回答
その他の回答 (1 件)
Mario Malic
2021 年 1 月 9 日
編集済み: Mario Malic
2021 年 1 月 9 日
Hello,
This should do the trick.
B = [NaN; cumsum(diff(A) ~= 0)]';
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!