Increase counter for each element in array
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Dario Walter
il 17 Giu 2020
Commentato: Dario Walter
il 18 Giu 2020
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 Commenti
Risposta accettata
madhan ravi
il 17 Giu 2020
ix = A(:) == unique(A);
B = nonzeros(cumsum(ix) .* ix)
6 Commenti
madhan ravi
il 18 Giu 2020
I knew you would come up with that question. That’s why you should experiment with 'stable' option in unique function.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!