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!
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!