2

我有两个矩阵,如下所示:

'01/01/2010'          1
'02/01/2010'          2
'03/01/2010'          3
'05/01/2010'         11
'06/01/2010'         17

'01/01/2010'          4
'02/01/2010'          5
'04/01/2010'          6
'05/01/2010'          7

,在 MATLAB 中做了一些棘手的事情后,我想创建以下三个矩阵:

'01/01/2010'          1          4
'02/01/2010'          2          5
'03/01/2010'          3        NaN
'04/01/2010'        NaN          6
'05/01/2010'         11          7
'06/01/2010'         17        NaN


'01/01/2010'          1          4
'02/01/2010'          2          5
'05/01/2010'         11          7

关于如何加入这些表的任何想法?干杯。

编辑:真的很抱歉我的错别字,伙计们。我更新了问题和输入/输出数据。请随时提供建议。

4

3 回答 3

9

我相信您要实现的目标在数据库世界中称为内部联接完全外部联接。

首先我们从两个数据集开始:

d1 = {
 '01/01/2010'          1
 '02/01/2010'          2
 '03/01/2010'          3
 '05/01/2010'         11
 '06/01/2010'         17
};
d2 = {
 '01/01/2010'          4
 '02/01/2010'          5
 '04/01/2010'          6
 '05/01/2010'          7
};

下面是执行这两种类型的连接的代码:

%# get all possible dates, and convert them to indices starting at 1
[keys,~,ind] = unique( [d1(:,1);d2(:,1)] );

%# full outer join
ind1 = ind(1:size(d1,1));
ind2 = ind(size(d1,1)+1:end);

fullOuterJoin = cell(numel(keys),3);
fullOuterJoin(:) = {NaN};           %# fill with NaNs
fullOuterJoin(:,1) = keys;          %# union of dates
fullOuterJoin(ind1,2) = d1(:,2);    %# insert 1st dataset values
fullOuterJoin(ind2,3) = d2(:,2);    %# insert 2nd dataset values

%# inner join
loc1 = ismember(ind1, ind2);
loc2 = ismember(ind2, ind1);

innerJoin = cell(sum(loc1),3);
innerJoin(:,1) = d1(loc1,1);        %# intersection of dates
innerJoin(:,2) = d1(loc1,2);        %# insert 1st dataset values
innerJoin(:,3) = d2(loc2,2);        %# insert 2nd dataset values

或者,我们可以通过简单地删除具有任何NaN值的行来从外连接数据集中提取内连接:

idx = all(~isnan(cell2mat(fullOuterJoin(:,2:end))), 2);
innerJoin = fullOuterJoin(idx,:);

无论哪种方式,结果:

>> fullOuterJoin
fullOuterJoin = 
    '01/01/2010'    [  1]    [  4]
    '02/01/2010'    [  2]    [  5]
    '03/01/2010'    [  3]    [NaN]
    '04/01/2010'    [NaN]    [  6]
    '05/01/2010'    [ 11]    [  7]
    '06/01/2010'    [ 17]    [NaN]

>> innerJoin
innerJoin = 
    '01/01/2010'    [ 1]    [4]
    '02/01/2010'    [ 2]    [5]
    '05/01/2010'    [11]    [7]
于 2012-06-12T15:09:39.160 回答
3

在 MATLAB 中,不能将字符串作为矩阵元素。为此,您需要使用元胞数组。这是使用元胞数组和containers.Maps 的解决方案。

FirstCellArray = {
'01/01/2010', 1;
'02/01/2010', 2;
'03/01/2010', 3;
'05/01/2010', 11;
'06/01/2010', 17
};

SecondCellArray = {
'01/01/2010', 4;
'02/01/2010', 5;
'04/01/2010', 6;
'05/01/2010', 7;
};

AllDatesCellArray = union(FirstCellArray(:,1), SecondCellArray(:,1));

% 为两个元胞数组创建containers.Maps。containers.Maps 是哈希表。

DateToFirstNumberMap = containers.Map(FirstCellArray(:,1), FirstCellArray(:,2));
DateToSecondNumberMap = containers.Map(SecondCellArray(:,1), SecondCellArray(:,2));

WithNaNsCellArray = AllDatesCellArray;

for Index = 1:size(WithNaNsCellArray, 1)
    Key = AllDatesCellArray{Index, 1};
    try
        NumberOne = cell2mat(values(DateToFirstNumberMap, cellstr(Key)));
    catch
        NumberOne = NaN;
    end
    WithNaNsCellArray{Index, 2} = NumberOne;
    try
        NumberTwo = cell2mat(values(DateToSecondNumberMap, cellstr(Key)));
    catch
        NumberTwo = NaN;
    end
    WithNaNsCellArray{Index, 3} = NumberTwo;
end

WithoutNaNsCellArray = WithNaNsCellArray;
NaNIndicesVector = (isnan([WithNaNsCellArray{:,2}]) | isnan([WithNaNsCellArray{:,3}]));
WithoutNaNsCellArray(NaNIndicesVector == 1, :) = [];

然后WithNaNsCellArray包含有NaN行的结果并WithoutNaNsCellArray包含没有NaN行的结果。

WithNaNsCellArray = 
'01/01/2010'    [  1]    [  4]
'02/01/2010'    [  2]    [  5]
'03/01/2010'    [  3]    [NaN]
'04/01/2010'    [NaN]    [  6]
'05/01/2010'    [ 11]    [  7]
'06/01/2010'    [ 17]    [NaN]

WithoutNaNsCellArray = 
'01/01/2010'    [ 1]    [4]
'02/01/2010'    [ 2]    [5]
'05/01/2010'    [11]    [7]
于 2012-06-11T15:28:14.753 回答
0

统计工具箱包含一个名为 JOIN 的函数,它基本上可以满足您的需求。

http://www.mathworks.de/de/help/stats/dataset.join.html

不幸的是,它可能无法处理字符串和多类型矩阵。但是您也许可以使用 JOIN 来缩短其他答案提出的解决方案。

于 2013-02-28T15:13:23.343 回答