Convert Matrix to Cell Arrays
32 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (1 件)
Geoff Hayes
2014 年 11 月 15 日
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!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!