Matlab从带有间隙的向量中制作矩阵 [英] matlab make matrix from vectors with gaps
问题描述
我有两组时间序列数据,第一列中按日期索引;值在第二位.日期以yyyymmdd格式表示(例如,Pearl Harbor Day是19411207),这两个时间序列重叠,但是两者都不是另一个子集,即使在重叠中,也可能缺少日期,并且由于格式的原因,存在间隔按数字顺序.由于这些日期最终只是整数,因此为了方便阅读,我将在下面替换一些小数字.
I have two sets of time-series data, indexed by dates in the first columns; values are in the second. Dates are represented in yyyymmdd format (e.g. Pearl Harbor Day is 19411207) The two time series overlap, but neither is a subset of the other, and even in the overlap, there may be dates missing, and due to the format, there are gaps in the numerical sequence. Since these dates end up just being integers, I will substitute small numbers below for ease of reading.
我想将这两个矩阵(26622x2和38067x2)转换为单个三列矩阵(事实证明是38103x3).
I want to turn these two matrices (a 26622x2 and a 38067x2) into a single three-column matrix (38103x3, as it turns out.)
我追求的合并是这样:
Index AVal Index BVal Index AVal BVal
1 2.5 3 6.5 1 2.5
2 3.4 + 5 8.9 = 2 3.4
4 5.6 7 9.1 3 6.5
5 7.8 8 7.1 4 5.6
7 8.00 5 7.8 8.9
7 8.00 9.1
8 7.1
在Excel中,我可以使用VLookup来做到这一点.我可以想象在Matlab中使用大量条件,循环和存储索引的方法,但是我想知道是否可能没有一组非常简单的联接/交叉类型命令来完成相同的任务.
In Excel, I would do this with VLookup. I can imagine an approach in Matlab that uses lots of conditionals, loops and stored indices, but I am wondering if there isn't possibly a very simple set of join/intersect-type commands that would accomplish the same thing.
有什么想法吗?
推荐答案
尝试使用setdiff
识别B
中的日期,而不是A
中的日期,然后与NaN
s或其他一些缺少值的值连接:
Try setdiff
to identify the dates in B
not in A
, then concatenate with NaN
s or some other value for missing values:
A = [1 2.5; 2 3.4; 4 5.6; 5 7.8; 7 8.0];
B = [3 6.5; 5 8.9; 7 9.1; 8 7.1];
[BnA,iB] = setdiff(B(:,1),A(:,1));
C = [A NaN(size(A,1),1); BnA NaN(numel(BnA),1) B(iB,2)]
C =
1.0000 2.5000 NaN
2.0000 3.4000 NaN
4.0000 5.6000 NaN
5.0000 7.8000 NaN
7.0000 8.0000 NaN
3.0000 NaN 6.5000
8.0000 NaN 7.100
然后使用intersect
处理这两个共同的值:
Then use intersect
to handle values that are common to both:
[AB,iA,iBA] = intersect(A(:,1),B(:,1));
C(iA,3) = B(iBA,2)
C =
1.0000 2.5000 NaN
2.0000 3.4000 NaN
4.0000 5.6000 NaN
5.0000 7.8000 8.9000
7.0000 8.0000 9.1000
3.0000 NaN 6.5000
8.0000 NaN 7.1000
然后使用sortrows
根据第一列进行排序:
Then use sortrows
to sort according to to the first column:
C = sortrows(C,1)
C =
1.0000 2.5000 NaN
2.0000 3.4000 NaN
3.0000 NaN 6.5000
4.0000 5.6000 NaN
5.0000 7.8000 8.9000
7.0000 8.0000 9.1000
8.0000 NaN 7.1000
这篇关于Matlab从带有间隙的向量中制作矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!