Convert Matrix to Cell Arrays

31 views (last 30 days)
Scott
Scott on 15 Nov 2014
Answered: Geoff Hayes on 15 Nov 2014
I would like to take a vector of numbers and creates several cell arrays, one from each row of the matrix.
i.e. I have the vector in this form
a = [1 2 3 4;5 6 7 8]
and would like it in this form
a = <1 2 3 4>,<5 6 7 8>
I have searched Google as well as the MathWorks website and cannot seem to find anything.
Any Ideas?

Answers (1)

Geoff Hayes
Geoff Hayes on 15 Nov 2014
Scott - try using mat2cell to convert your matrix to a cell array. In your case, that might be
a = [1 2 3 4 ; 5 6 7 8];
aAsCell = mat2cell(a,[1 1],[4]);
to produce
aAsCell =
[1x4 double]
[1x4 double]
Since there are two rows that we want to separate into cell arrays, we set the first dimension distribution as [1 1] (note how this distribution sums to the number of rows of a). Since there is no change to the column (second) dimension distribution, then we leave this as [4] (in this case, since there is no change to the column distribution we don't really need to add this...but will do so just to make clear how this works).
If you wish to get the output as a row vector cell array, then just do perform a transpose with the apostrophe
aAsCell = mat2cell(a,[1 1],[4])';
Try the above and see what happens!

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!