Search code examples
matlabcell

Create a cell with values from multiple cells


I have P being a 1x23 cell. In each cell of P, there is a bunch of numbers in nx1 dimension. Cells in P do not have the same row n (e.g., P{1,1} could be 16x1 and P{1,2} could be 17x1. Now, I'd like to put all the values from all the cells in P (P{1,1}, P{1,2}...P{1,23}) into cell D with the dimension of mx1. m never exceeds 1080 so I can do D=cell(1080,1) then eliminate empty cells later. Right now I am having trouble inputting all the values of P into D. Could anyone help?

Thanks.


Solution

  • Is this what you want?

    >> P = {[1 2].', [3 4 5].'}
    >> D = vertcat(P{:})
    D =
         1
         2
         3
         4
         5
    

    If you really need D in cell form:

    >>  D = mat2cell(D,ones(1,size(D,1)),1)
    D = 
        [1]
        [2]
        [3]
        [4]
        [5]