我有两个矩阵,如下所示:
'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
关于如何加入这些表的任何想法? 欢呼声。
编辑:真的很抱歉我的错别字,伙计们。我更新了问题和输入/输出数据。请随时提供建议。答案 0 :(得分: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]
答案 1 :(得分:3)
在MATLAB中,不能将字符串作为矩阵元素。为此,您需要使用单元格数组。这是使用单元格数组和containers.Map
s。
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.Map
。 containers.Map
是哈希表。
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]
答案 2 :(得分:0)
统计工具箱包含一个名为JOIN的函数,基本上可以完成你想要的工作。
http://www.mathworks.de/de/help/stats/dataset.join.html
不幸的是,它可能无法处理字符串和多型矩阵。但是你可以使用JOIN来缩短其他答案提出的解决方案。