Increase counter for each element in array

1 view (last 30 days)
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!

Accepted Answer

madhan ravi
madhan ravi on 17 Jun 2020
ix = A(:) == unique(A);
B = nonzeros(cumsum(ix) .* ix)
  6 Comments
madhan ravi
madhan ravi on 18 Jun 2020
I knew you would come up with that question. That’s why you should experiment with 'stable' option in unique function.
Dario Walter
Dario Walter on 18 Jun 2020
Well done Madhan :). Thanks a lot!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!