Index same value numbers in array

1 vue (au cours des 30 derniers jours)
James Watson
James Watson le 20 Fév 2018
Ok, so let's say I have array A = {1 1 1 5 5 8 8 8 8 9 10 11}. I want to get array containing this: B = {1 2 3 1 2 1 2 3 4 1 1 1}. Array A is already sorted, I want to index same values until new value and then start all over again. I want to make it work without for loop. I thought about dividing array into cells using unique and mat2cell and then probably cellfun to get indexes of values in each cell? Either way, anyone can help me to find best way to solve it?

Réponses (1)

Jos (10584)
Jos (10584) le 20 Fév 2018
I offer you my function runindex which does exactly this, fast and vectorised!
A = [1 1 1 5 5 8 9 9 9 9 10 11]
B = runindex(A)
%-> [1 2 3 1 2 1 1 2 3 4 1 1]
runindex can be downloaded for free from the Matlab File Exchange:
... and if you find it useful, please give some feedback :)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by