本文介绍了两个表的完全外部联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,例如price(5行,1列)和pricedate(6行,1列),它们没有任何共同点.我想获得完全的外部联接,以便我的新表ABC有30行,基本上,pricedate中的每一行都具有所有价格.

I have two tables, say, price (5 rows, 1 column) and pricedate (6 rows, 1 column), they don't have anything in common. I want to obtain full outer join, so that my new table ABC has 30 rows, basically, every row in pricedate has all prices.

该怎么做?我可以使用完全外部联接或其他方法吗?

How to do that? Can I use full outer join or something else?

推荐答案

我的回答涉及两种情况:

My answer covers two cases:

  1. P (价格)PD (标价)
  2. P (价格)PD (标价)数组
  1. P (prices) and PD (pricedates) are tables
  2. P (prices) and PD (pricedates) are arrays

这是代码:

% generate some sample data
price      = [1;2;3;4;5];
pricedate1 = [11;12;13;14;15;16];
pricedate2 = pricedate1+10;
pricedate3 = pricedate2+10;

% create sample table
P  = table(price);
PD = table(pricedate1,pricedate2,pricedate3);

% this is the code if P and PD are tables
TMP = repmat(P{:,1},1,size(PD,1))';
T = [repmat(PD,size(P,1),1),table(TMP(:),'VariableNames',{'price'})]

% create sample arrays
P  = price;
PD = [pricedate1,pricedate2,pricedate3];

% this is the code if P and PD are arrays
TMP = repmat(P,1,size(PD,1))'
T = [repmat(PD,size(P,1),1), TMP(:)]

可以将其写在单行中以消除TMP-变量.

It is possible to write it in a single line to to eliminate the TMP-variable.

对于table类型的数据:

T = [repmat(PD,size(P,1),1),table(subsref(repmat(P{:,1},1,size(PD,1))',struct('type','()','subs',{{':'}})),'VariableNames',{'price'})]

对于array类型的数据:

T = [repmat(PD,size(P,1),1),subsref(repmat(P,1,size(PD,1))',struct('type','()','subs',{{':'}}))];

我承认,单行看起来很神秘.

I admit, that the single line looks quite cryptic.

这篇关于两个表的完全外部联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 06:38