Increase counter for each element in array

1 次查看(过去 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!

采纳的回答

madhan ravi
madhan ravi 2020-6-17
ix = A(:) == unique(A);
B = nonzeros(cumsum(ix) .* ix)
  6 个评论
madhan ravi
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 CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by