Index same value numbers in array

조회 수: 1 (최근 30일)
James Watson
James Watson 2018년 2월 20일
답변: Jos (10584) 2018년 2월 20일
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