Combine elements across two vectors

2 次查看(过去 30 天)
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!

采纳的回答

Guillaume
Guillaume 2014-9-22
[m, ~, indices] = unique(m, 'stable'); %stable optional if you don't mind your data sorted
n = accumarray(indices, n)';

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by