【问题标题】:MATLAB - Intersection of matrices with same number of rows, different number of columnsMATLAB - 具有相同行数、不同列数的矩阵的交集
【发布时间】:2014-10-15 17:01:46
【问题描述】:

如何使行数相同但列数不同的10矩阵相交?

目标是找到10 矩阵的公共列。

如何减少这些线条:

B1 = intersect(A1',A2','rows')';
B2 = intersect(B1',A3','rows')';
B3 = intersect(B2',A4','rows')';
B4 = intersect(B3',A5','rows')';
B5 = intersect(B4',A6','rows')';
B6 = intersect(B5',A7','rows')';
B7 = intersect(B6',A8','rows')';
B8 = intersect(B7',A9','rows')';
B9 = intersect(B8',A10','rows')';

【问题讨论】:

  • 翻转它们,使它们具有共同的行而不是共同的列,将它们堆叠起来并使用 uniquerows 选项。
  • @nkjt:它给出了联合,而不是交集!

标签: matlab matrix


【解决方案1】:

假设A1A2A3....A10 是这 10 个输入矩阵,看看这是否适合你 -

A = cat(1,{A1},{A2},{A3},.....{A10}) %// concatenate all variables into a cell array
A = cellfun(@(x) x.',A,'uni',0) %//'# transpose each cell, so that number of columns 
                               %// corresponding to all matrices is the same

%// Get intersecting rows for each pair from A and keeping the intersecting output 
%// for comparison against the next one.
out = A{1}; %// iInitiliaze with the first cell that is the first matrix
for iter = 2:numel(A)
    out = intersect(A{iter},out,'rows'); %// find intersection for each matrix 
                                        %// against the running intersection result
end    
result = out.' %//# desired output

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多