Combine elements across two vectors
2 views (last 30 days)
Show older comments
Alexandra Maxim
on 22 Sep 2014
Commented: Alexandra Maxim
on 22 Sep 2014
I have two vectors, such that one is the time index and the second is how many times it occurred:
m=[10 20 20 30] n=[3 4 5 6]
I would like to combine, the two occurrences at time 20.
Essentially I need:
m=[10 20 30] n=[3 9 6] <-- here 4+5 were added because they both occurred in time 20.
Any suggestions on how to achieve this with code? I am trying to avoid a for loop...
Thanks in advance!
0 Comments
Accepted Answer
Guillaume
on 22 Sep 2014
[m, ~, indices] = unique(m, 'stable'); %stable optional if you don't mind your data sorted
n = accumarray(indices, n)';
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices 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!