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

1 view (last 30 days)
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.

Accepted Answer

Oleg Komarov
Oleg Komarov on 3 Aug 2011
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 Comment
Neil M
Neil M on 4 Aug 2011
Beautiful. Thank you for the extremely quick response, this appears to work perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!