在Matlab中相交几个不同长度的表格

问题描述:

我在Matlab中有更多的8+表格长度不同。它们都在第一列中包含日期。我想在日期栏中获得所有这些表格的交集。有3个表下面的小例子,显示了我想要的:在Matlab中相交几个不同长度的表格

Date1=datenum(2011,6,7,0:24:240,0,0).'; 
Date2=datenum(2011,6,8,0:24:240,0,0).'; 
Date3=datenum(2011,6,5,0:24:240,0,0).'; 


T1 = table(Date1,ones(11,1),'VariableNames',{'Date','Var1'}) 
T2 = table(Date2,ones(11,1)*2,'VariableNames',{'Date','Var2'}) 
T3 = table(Date3,ones(11,1)*3,'VariableNames',{'Date','Var3'}) 

因此,我想下面的输出:

Date  Var1 Var2 Var3 
______ ____ ____ ____ 

734662 1  2  3 
734663 1  2  3 
734664 1  2  3 
734665 1  2  3 
734666 1  2  3 
734667 1  2  3 
734668 1  2  3 
734669 1  2  3 

有没有在Matlab中的函数可以做到这一点?

intersect函数可能适用于您的情况。

我不知道你如何使用表格,但下面的代码应该让你找到每个DateN矢量的交集索引。一旦你有了索引,你就可以重建一个只包含所有表之间公共索引的全局表。

[C,ia,ib ] = intersect(Date1,Date2) ; %// get indices of intersection of first 2 vectors (Date1&2) 
[D,ja,ix3] = intersect( C ,Date3) ; %// get indices of intersection of last result (Date1&2) with last vector (Date 3) 

ix1 = ia(ja) ; %// take only the common indices of the 2 intersection operations (For Date1) 
ix2 = ib(ja) ; %// take only the common indices of the 2 intersection operations (For Date2) 

%// Build the "common" table 
intersectionTable = [ Date1(ix1) Var1(ix1) Var2(ix2) Var3(ix3) ] ; 
+0

感谢。不过,有8个以上的表格,这个解决方案有点麻烦。有什么建议可以实现一次完成所有交集的功能? – Mace 2014-12-03 11:54:13

+0

哈哈,肯定对8表这将会有相当多的代码行重复。对于你的8个表(或者实际上任何数字),如果你明白代码在做什么,你可以将它嵌入到一个循环中,然后可以根据需要将它们相交。 – Hoki 2014-12-03 11:56:35

+0

是的,谢谢! – Mace 2014-12-03 11:58:49

我写了一个非常紧密相关的代码,另外一个问题,我在这里放下抛光功能的代码,发现在同一时间数列的交叉元件和相应的指标,而不诉诸于任何一种循环的为计算部分。下面的代码 -

function [out_val,out_idx] = intersect_arrays(varargin) 

%// Concatenate all vector arrays into a 2D array 
if isrow(varargin{1}) 
    M = vertcat(varargin{:}); 
else 
    M = horzcat(varargin{:}).'; %//' 
end 

%// Find unique values for all elements in all arrays 
unqvals = unique(M(:),'stable')'; %//' 

%// Find unqiue elements common across all arrays (intersecting elements) 
out_val = unqvals(all(any(bsxfun(@eq,M,permute(unqvals,[1 3 2])),2),1)); 

%// Find first indices across all arrays holding the intersecting elements 
[~,idx] = max(bsxfun(@eq,M,permute(out_val,[1 3 2])),[],2); 
out_idx = squeeze(idx).'; %//' 

return 

现在,为了解决你的情况,我们可以使用的功能代码,像这样 -

num_arrays = 3; %// Number of arrays to be used 

%// Find intersecting elements and their corresppinding indices in each array 
[int_ele,int_idx] = intersect_arrays(T1.Date,T2.Date,T3.Date) %// Add inputs here 

%// Create an array of all Var data 
all_idx = cat(2,T1.Var1,T2.Var2,T3.Var3)      %// Add inputs here 

%// Select Var data based on the intersecting indices 
select_idx = all_idx(bsxfun(@plus,int_idx,[0:num_arrays-1]*size(T1.Var1,1))) 

%// Output results as a numeric array and table 
out_array = [int_ele(:) select_idx] 
out_table = cell2table(num2cell(out_array),'VariableNames',... 
             {'Date','Var1','Var2','Var3'}) 

输出 -

out_table = 
    Date  Var1 Var2 Var3 
    ______ ____ ____ ____ 
    734662 1  2  3 
    734663 1  2  3 
    734664 1  2  3 
    734665 1  2  3 
    734666 1  2  3 
    734667 1  2  3 
    734668 1  2  3 
    734669 1  2  3