Converting a cell array to a multidimensional array

11 ビュー (過去 30 日間)
Bryan Wilson
Bryan Wilson 2021 年 12 月 20 日
コメント済み: Voss 2021 年 12 月 20 日
I have a 1xK cell array of MxN doubles. I need to covert it to a multidimentional array of NxMxK doubles. I should be able to do this without a "for loop" using some combination of cellfun, reshape, and permute. For the life of me I can't figure it out. Note: I saw a very similar question here (how-to-convert-from-cell-array-to-multidimensional-array).
K = 6;
M = 1000;
N = 4;
A = repmat({rand(M,N)},1,K);
%B = ?
% size(B)
% ans =
% 4 1000 6

採用された回答

Voss
Voss 2021 年 12 月 20 日
AA = cellfun(@(x)x.',A,'UniformOutput',false); % transpose each element of A
B = cat(3,AA{:}); % concatenate along the third dimension
  2 件のコメント
Bryan Wilson
Bryan Wilson 2021 年 12 月 20 日
Thanks Benjamin.
Your previous answer that you deleted also works if you add a permute after the cat.
B = cat(3,A{:});
B = permute(B,[2 1 3]);
Voss
Voss 2021 年 12 月 20 日
That's true, and it's probably more efficient.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by