Convert cell array of cell arrays to matrix of matrices

后端 未结 5 1271
一向
一向 2021-01-05 05:37

I can convert a cell array of matrices to matrix:

>> C={[1,1]; [2,2]; [3,3]};
>> cell2mat(C)
ans =
     1     1
     2     2
     3     3
         


        
5条回答
  •  伪装坚强ぢ
    2021-01-05 05:50

    This should do the trick:

    cellOfCells = {{1,1}; {2,2}; {3,3}};
    cell2mat(cellfun(@cell2mat, cellOfCells, 'UniformOutput', false))
    

    Edit:

    I agree that keeping it simple is an important, but so is having fun :) So - here's a one-liner that should do the trick (and can be easily generalized for any size):

    a = {{1,1;1,1}; {2,2;2,2}; {3,3;3,3}}
    reshape(cell2mat(cellfun(@cell2mat,a, 'UniformOutput', false))', 2, 2, 3)
    

提交回复
热议问题