Pad with Nan according to maximum row in cell

33 ビュー (過去 30 日間)
Cside
Cside 2019 年 10 月 27 日
コメント済み: Tobias 2023 年 3 月 14 日
Hi, I have a 1 x 8 cell (attached) and would like to cell2mat this matrix i.e to expand and show into one big numeric 97 x 8 array (97 being the maximum length of a cell array). I would also like to pad the ends of the smaller cell arrays with Nan to make all columns the same row length. How may I write for this?
Thank you!
  1 件のコメント
Stephen23
Stephen23 2023 年 3 月 3 日
編集済み: Stephen23 2023 年 3 月 3 日
"How may I write for this?"
Rather than reinventing the wheel, you can simply download Jos' excellent PADCAT here:
and use it like this, where C is your cell array:
M = padcat(C{:})
This works automatically for vectors in either orientation.

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

採用された回答

Fabio Freschi
Fabio Freschi 2019 年 10 月 27 日
I keep all steps separated
% the data
A = {rand(47,1),rand(80,1),rand(97,1)};
% pad with NaNs
A = cellfun(@(x)[x(1:end); NaN(97-length(x),1)],A,'UniformOutput',false);
% make a matrix
A = cell2mat(A);
  3 件のコメント
Fabio Freschi
Fabio Freschi 2023 年 3 月 2 日
Joanne,
if I understand correctly your question, you must simply change everything that is row into column and vice versa
% the data
A = {rand(1,47);rand(1,80);rand(1,97)}; % <- note the semicolon, note the row/col excange in the data
% pad with NaNs
A = cellfun(@(x)[x(1:end) NaN(1,97-length(x))],A,'UniformOutput',false); % <- note the missing semicolon after x(1:end) and the row/col excange in NaN
% make a matrix
A = cell2mat(A);
Hope iot helps
Tobias
Tobias 2023 年 3 月 14 日
Hi! Thank you for your answer, unfortunately i appears that for cells < max_t the function just adds another cell arrayto the end of it, with max_t size. Do you know how to circumvent this ?

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

その他の回答 (0 件)

カテゴリ

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