Index same value numbers in array

1 次查看(过去 30 天)
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?

回答(1 个)

Jos (10584)
Jos (10584) 2018-2-20
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 :)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by