简体   繁体   English

Matlab从带有间隙的向量中制作矩阵

[英]matlab make matrix from vectors with gaps

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 (eg 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. 日期以yyyymmdd格式表示(例如,Pearl Harbor Day是19411207),这两个时间序列重叠,但是两者都不是另一个子集,即使重叠,也可能缺少日期,并且由于格式的原因,存在间隔按数字顺序。 Since these dates end up just being integers, I will substitute small numbers below for ease of reading. 由于这些日期最终只是整数,因此为了便于阅读,我将在下面替换一些小数字。

I want to turn these two matrices (a 26622x2 and a 38067x2) into a single three-column matrix (38103x3, as it turns out.) 我想将这两个矩阵(26622x2和38067x2)转换为单个三列矩阵(事实证明是38103x3)。

The merge I am after is this: 我要的合并是这样的:

 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

In Excel, I would do this with VLookup. 在Excel中,我可以使用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. 我可以想象在Matlab中使用大量条件,循环和存储的索引的方法,但是我想知道是否可能没有一组非常简单的联接/交叉类型命令来完成相同的任务。

Any thoughts? 有什么想法吗?

Try setdiff to identify the dates in B not in A , then concatenate with NaN s or some other value for missing values: 尝试使用setdiff来确定B的日期,而不是A的日期,然后将NaN或其他一些值连接起来以获取缺失值:

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

Then use intersect to handle values that are common to both: 然后使用intersect来处理两个共同的值:

[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

Then use sortrows to sort according to to the first column: 然后使用sortrows根据第一列进行排序:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM