Create new vector from data - Counts of element locations in vector

I have kind of a strange specific need for transforming my data to use in a script a colleague has sent me. I'm new to MATLAB, so changing my data should be easier than rearranging the guts of the code.
What I need is to change a vector of numbers (between 0 and 5) to a new vector with 'counts' of the location of the number in the original vector. That's confusing, so for an example:
x = [0,1,0,2,0,3,0,1,1,4]
would become:
y = [2,4,4,6,6,6,8,9,10,10,10,10]
Where the first '2' in y is the location of the '1' at the second location in vector x, etc.
Other than a subset of the find and sum functions, I'm stumped. Thanks in advance for any help, I'm still familiarizing myself with the software.

 채택된 답변

Oleg Komarov
Oleg Komarov 2011년 8월 3일
You have to use run-lenght decoding algorithm:
x = [0,1,0,2,0,3,0,1,1,4]
val = find(x);
len = x(val);
rude(len,val)
You can find rude here.

댓글 수: 1

Beautiful. Thank you for the extremely quick response, this appears to work perfectly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by