Convert Matrix to Cell Arrays

32 ビュー (過去 30 日間)
Scott
Scott 2014 年 11 月 15 日
回答済み: Geoff Hayes 2014 年 11 月 15 日
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?

回答 (1 件)

Geoff Hayes
Geoff Hayes 2014 年 11 月 15 日
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!

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by