Increase counter for each element in array
2 ビュー (過去 30 日間)
古いコメントを表示
Hey,
given A = [1 1 3 4 5 6 6 7 7 7]. How could I return the sequence B =[1 2 1 1 1 1 2 1 2 3] such that each duplicate is counted and B is the same size as A?
I appreciate your help!
0 件のコメント
採用された回答
madhan ravi
2020 年 6 月 17 日
ix = A(:) == unique(A);
B = nonzeros(cumsum(ix) .* ix)
6 件のコメント
madhan ravi
2020 年 6 月 18 日
I knew you would come up with that question. That’s why you should experiment with 'stable' option in unique function.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!