If a=[1 2 3], b=[10 20 30], how to create c=[10 20 20 30 30 30]?
Show older comments
The b matrix i'th element is repeated the number of times the given number in 'a' matrix and placed in matix 'c'. Also if a=[0 1 2], b=[10 20 30] how to get c=[20 30 30]??
Accepted Answer
More Answers (2)
Roger Stafford
on 14 Jul 2014
Edited: Roger Stafford
on 14 Jul 2014
Another way:
t = accumarray(cumsum([1,a]).',1);
c = b(cumsum(t(1:end-1)));
2 Comments
Jan
on 14 Jul 2014
I assume you mean "t = " instead of "t - "
Roger Stafford
on 14 Jul 2014
Oops! Yes, thank you Jan.
Jan
on 14 Jul 2014
c = RunLength(a, b)
Categories
Find more on Linear Algebra 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!