Create new vector from data - Counts of element locations in vector
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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)
More Answers (0)
See Also
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!