How to count the number of times that values changes?
18 次查看(过去 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)]';
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!