【问题标题】:Intersecting several tables of different lengths in Matlab在Matlab中相交几个不同长度的表格
【发布时间】:2014-12-03 11:02:14
【问题描述】:

我在Matlab 中有更多 8 个以上不同长度的表格。它们都在第一列中包含日期。我想在日期列上获得所有这些表的交集。以下带有 3 个表的小示例显示了我想要的内容:

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 中有没有可以做到这一点的函数?

【问题讨论】:

    标签: matlab merge intersection


    【解决方案1】:

    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) ] ;
    

    【讨论】:

    • 谢谢。但是,拥有 8 个以上的表,这个解决方案有点麻烦。关于实现一次完成所有交集的功能的任何建议?
    • 哈哈,当然对于 8 表,这将是相当多的代码行要重复。对于您的 8 个表格(或实际上的任何数字),如果您了解代码在做什么,您可以将其嵌入到一个循环中,该循环可以与您需要的任意数量的表格相交。
    • @Mace 查看other posted solution 是否相同?
    【解决方案2】:

    我为另一个问题写了一个非常相关的代码,我在这里写了一个优美的函数代码,它可以同时找到几个数组的相交元素和相应的索引,而不需要对计算部分使用任何类型的循环.这是代码-

    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  
    

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 1970-01-01
      • 2018-10-31
      • 2017-01-15
      • 2018-11-21
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多