Index same value numbers in array

1 view (last 30 days)
James Watson
James Watson on 20 Feb 2018
Answered: Jos (10584) on 20 Feb 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?

Answers (1)

Jos (10584)
Jos (10584) on 20 Feb 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 :)

Products

Community Treasure Hunt

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

Start Hunting!