how to convert cell to double

15 ビュー (過去 30 日間)
Farshid Daryabor
Farshid Daryabor 2020 年 1 月 24 日
編集済み: Adam Danz 2022 年 1 月 19 日
I have problem to convert cell (attached file) to double, thanks in advance for any help.

採用された回答

Adam Danz
Adam Danz 2020 年 1 月 24 日
編集済み: Adam Danz 2022 年 1 月 19 日
There are 12 elements in your cell array. Each element is a matrix but of different sizes.
OUT =
1×12 cell array
Columns 1 through 7
{73×6 double} {1581×12 double} {105×6 double} {105×6 double} {113×6 double} {264×6 double} {98×7 double}
Columns 8 through 12
{98×6 double} {98×7 double} {88×6 double} {994×3 double} {194×6 double}
If you want to combine all elements of your cell array some common options are
  1. convert them to a single vector (row or column)
  2. equate the number of columns in all matrices and fill the missing elements with NaN or another missing value indicator. Then concatenate the matrices vertically
  3. equate the number of rows in all matrices, fill the missing elements, and concatenate horizontally
  4. equate the number of rows and columns in all matrices, fill the missing elements, and concatenate in a 3D array.
% OUT is the 1xn cell array where each element contains a matrix of different sizes.
% METHOD: put all values into a column vector
% Transpose the result for a row vector
m = cell2mat(cellfun(@(c){c(:)},OUT)');
% METHOD: Concatenate matrices vertically, fill missing columns with NaN.
maxNumCol = max(cellfun(@(c) size(c,2), OUT)); % max number of columns
mPad = cell2mat(cellfun(@(c){padarray(c,[0,maxNumCol-size(c,2)],NaN,'Post')},OUT)');
% METHOD: Concatenate matrices horizontally, fill missing rows with NaN.
maxNumRow = max(cellfun(@(c) size(c,1), OUT)); % max number of rows
mPad = cell2mat(cellfun(@(c){padarray(c,[maxNumRow-size(c,1),0],NaN,'Post')},OUT));
% METHOD: Concatenate matrices along 3rd dimension, fill missing rows with NaN.
maxMatSz = max(cell2mat(cellfun(@(c) {size(c)}, OUT)'),[],1); % [max rows, max cols]
cPad = cellfun(@(c){padarray(c,[maxMatSz(1)-size(c,1),maxMatSz(2)-size(c,2)],NaN,'Post')},OUT);
mPad = cat(3, cPad{:});
  2 件のコメント
Adam Danz
Adam Danz 2020 年 1 月 24 日
Farshid Daryabor's answer moved here as a comment.
Thanks
Adam Danz
Adam Danz 2020 年 1 月 24 日
Glad I could help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by