# How to convert a cell array (nx1) with different size of elements in each cell to a matrix?

5 views (last 30 days)
Sim on 3 Oct 2012
Suppose I have a nx1 cell as follows:
X = [0,160,208]
[272,132]
[1272,133]
[22724,310]
[27516,312]
[30424,374,687]
[32262,437]
[36288,299,574,690]
[47884,637]
[53542,425]
[54494,633]
[58420,77]
[62800,461]
[72252,239]
[78429,46]
I want something like:
newX = [ 0 160 208
272 132
1272 133
22724 310
27516 312
30424 374 687
32262 437
36288 299 574 690
47884 637
53542 425
54494 633
58420 77
62800 461
72252 239
78429 46 ]
That is each element of the cell X should be stored in new column of the newX.
(I have tried something with pre-defining the new array but I do not want any NaN or Zeros after I pre-define it. )
Your help will be greatly appreciated as I am very new to Matlab.
Thank you
Matt Fig on 4 Oct 2012
So if you do not want a nan or a zero in the non-used spots, what do you want there? MATLAB will not allow an empty element of a numeric array. If an array is numeric, this means that each element of the array is numeric...

Walter Roberson on 3 Oct 2012
That is not possible in MATLAB. MATLAB does not support "ragged arrays". If you want an array, then the unused locations must be populated with something
Walter Roberson on 4 Oct 2012
Note that if your goal is just to display it with different numbers of elements per line, then that is feasible.