gpt4 book ai didi

matlab - 在 MATLAB 中连接矩阵

转载 作者:行者123 更新时间:2023-12-02 07:08:04 25 4
gpt4 key购买 nike

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

'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

知道如何加入这些表吗?干杯。

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

最佳答案

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

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

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]

关于matlab - 在 MATLAB 中连接矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10981570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com