根据时间值合并2个向量 [英] Merge 2 vectors according their time values
本文介绍了根据时间值合并2个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据它们的时间值合并2个向量.看起来应该像这样(第1列=时间,第2列=实际值):
I would like to merge 2 vectors according their time values. This should look like this (column 1 = time, column 2 = actual value):
A =
1 234
3 121
4 456
6 6756
B =
2 435
5 90
10 365
结果:
C =
1 234
2 435
3 121
4 456
5 90
6 6756
10 365
在Matlab中是否有一种优雅的方法来实现这一点?
Is there an elegant way to realize this in Matlab?
推荐答案
这里很简单:
C = sortrows([A;B])
C =
1 234
2 435
3 121
4 456
5 90
6 6756
10 365
请注意,这假设第1列中的所有时间值都是唯一的.如果不是这种情况,则可以使用accumarray
:
Note that this assumes that all of the time values in column 1 are unique. If this is not the case, you can use accumarray
:
A =
1 234
3 121
4 456
6 6756
B =
2 435
5 90
10 365
B = [B; 1 512]
B =
2 435
5 90
10 365
1 512
C = [A;B];
D = accumarray(C(:,1),C(:,2));
U = unique(C(:,1));
E = [U,D(U)]
E =
1 746 %// 764 = 234 + 512
2 435
3 121
4 456
5 90
6 6756
10 365
这篇关于根据时间值合并2个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文