首页 文章

如何在MATLAB中将单元阵列中不同大小的矩阵组合成矩阵

提问于
浏览
1

How to combine vectors of different length in a cell array into matrix in MATLAB类似,我想将存储在单元阵列中的具有不同维度的矩阵组合成具有零而不是空白空间的矩阵 . 具体来说,我有一个单元阵列{1,3}有3个矩阵大小(3,3)(4,3)(4,3):

A={[1 2 3; 4 5 6; 7 8 9]  [1 2 3; 4 5 6; 7 8 9; 9 9 9]  [1 2 3; 4 5 6; 7 8 9; 4 4 4]}

我想获得类似的东西:

B =

 1     2     3     1     2     3     1     2     3
 4     5     6     4     5     6     4     5     6
 7     8     9     7     8     9     7     8     9
 0     0     0     9     9     9     4     4     4

我尝试使用cellfun和cell2mat,但我不知道如何做到这一点 . 谢谢 .

5 回答

  • 0

    如果有可能在一行或几行中我会感到惊讶 . 你可能不得不自己做一些循环 . 以下是在不兼容的第一个尺寸长度的特定情况下实现的目标:

    A={[1 2 3; 4 5 6; 7 8 9]  [1 2 3; 4 5 6; 7 8 9; 9 9 9]  [1 2 3; 4 5 6; 7 8 9; 4 4 4]}
    
    maxsize = max(cellfun(@(x) size(x, 1), A));
    B = A;
    for k = 1:numel(B)
        if size(B{k}, 1) < maxsize
            tmp = B{k};
            B{k} = zeros(maxsize, size(tmp,1));
            B{k}(1:size(tmp,1),1:size(tmp,2)) = tmp;
        end
    end
    
    B = cat(2, B{:});
    

    现在B是:

    B =
    
         1     2     3     1     2     3     1     2     3
         4     5     6     4     5     6     4     5     6
         7     8     9     7     8     9     7     8     9
         0     0     0     9     9     9     4     4     4
    
  • 0

    即使其他答案都很好,我也想提交我的,使用 cellfun .

    l = max(cellfun(@(x) length(x),A))
    
    B = cell2mat(cellfun(@(x) [x;zeros(l-length(x),3)], A, 'UniformOutput', 0));
    
  • 2

    使用bsxfun的屏蔽功能 -

    %// Convert A to 1D array
    A1d = cellfun(@(x) x(:).',A,'Uni',0) %//'
    
    %// Get dimensions of A cells
    nrows = cellfun('size', A, 1)
    ncols = cellfun('size', A, 2)
    
    %// Create a mask of valid positions in output numeric array, where each of
    %// those numeric values from A would be put
    max_nrows = max(nrows)
    mask = bsxfun(@le,[1:max_nrows]',repelem(nrows,ncols))  %//'
    
    %// Setup output array and put A values into its masked positions
    B = zeros(max_nrows,sum(ncols))
    B(mask) = [A1d{:}]
    

    Sample run

    输入 -

    A={[1 2 3 5 6; 7 8 9 3 8]  [1 2 3; 4 5 6; 7 8 9; 9 9 9]  [1 2 3; 4 5 6; 7 8 9; 4 4 4]}
    

    输出 -

    B =
         1     2     3     5     6     1     2     3     1     2     3
         7     8     9     3     8     4     5     6     4     5     6
         0     0     0     0     0     7     8     9     7     8     9
         0     0     0     0     0     9     9     9     4     4     4
    
  • 0

    我会使用一个很好的for循环来实现它,我认为这非常直观 .

    这是评论的代码:

    clc;clear var
    
    
    A={[1 2 3; 4 5 6; 7 8 9]  [1 2 3; 4 5 6; 7 8 9; 9 9 9]  [1 2 3; 4 5 6; 7 8 9; 4 4 4]};
    
    %// Find the maximum rows and column # to initialize the output array.
    MaxRow = max(cell2mat(cellfun(@(x) size(x,1),A,'Uni',0)));
    SumCol = sum(cell2mat(cellfun(@(x) size(x,2),A,'Uni',0)));
    
    B = zeros(MaxRow,SumCol);
    
    %// Create a counter to keep track of the current columns to fill
    ColumnCounter = 1;
    for k = 1:numel(A)    
        %// Get the # of rows and columns for each cell from A
        NumRows = size(A{k},1);
        NumCols = size(A{k},2);
    
        %// Fill the array
        B(1:NumRows,ColumnCounter:ColumnCounter+NumCols-1) = A{k};
    
        %// Update the counter
        ColumnCounter = ColumnCounter+NumCols;
    end
    disp(B)
    

    输出:

    B =
    
         1     2     3     1     2     3     1     2     3
         4     5     6     4     5     6     4     5     6
         7     8     9     7     8     9     7     8     9
         0     0     0     9     9     9     4     4     4
    
  • 5
    [max_row , max_col] = max( size(A{1}) , size(A{2}) , size(A{3}) );
    A{1}(end:max_row , end:max_col)=0;
    A{2}(end:max_row , end:max_col)=0;
    A{3}(end:max_row , end:max_col)=0;
    B=[A{1} A{2} A{3}];
    

相关问题