How can I count the followed occurrences of each element in a vector in MATLAB?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi, I want to count the number of followed occurrences of each element in a vector.
So if my input is
x = [1 1 1 2 2 1 1 2 5 5]
I need an output
y = [1 2 1 2 5;3 2 2 1 2] How do I do this?
2 件のコメント
William
2015 年 11 月 30 日
check out this post : http://www.mathworks.com/matlabcentral/answers/34302-how-to-find-consecutive-numbers
採用された回答
Stephen23
2015 年 11 月 30 日
編集済み: Stephen23
2015 年 11 月 30 日
x = [1 1 1 2 2 1 1 2 5 5];
f = [find(diff(x)),numel(x)];
y(2,:) = [f(1),diff(f)];
y(1,:) = x(f);
which generates this output:
>> y =
1 2 1 2 5
3 2 2 1 2
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!