フィルターのクリア

Place each dimension of matrix into a cell array

1 回表示 (過去 30 日間)
Fernando Duarte
Fernando Duarte 2011 年 4 月 5 日
Given a matrix with 3 non-singleton dimensions, how can I place each 2d matrix (along dimension 1) into an element of a cell array?
Concretely:
Given
A(:,:,1) = [1 2; 3 4] A(:,:,2) = [5 6; 7 8]
How do you construct a cell array B such that
B{1} = [1 2; 3 4] B{2} = [5 6; 7 8]
Thanks! (I can write a for loop, but that seems awfully inefficient)

採用された回答

Matt Fig
Matt Fig 2011 年 4 月 5 日
B = squeeze(mat2cell(A,2,2,[1 1]))
This is probably quicker:
mat2cell(reshape(A,2,4),2,[2 2])
More generally:
A(:,:,1) = [1 2; 3 4;5 6;45 50];
A(:,:,2) = [7 8;9 10;11 12;13 14];
A(:,:,3) = [70 80;90 100;110 120;130 140];
S = size(A);
B = mat2cell(reshape(A,S(1),S(2)*S(3)),S(1),ones(1,S(3))*S(2))
  1 件のコメント
Fernando Duarte
Fernando Duarte 2011 年 4 月 5 日
Great answer, short and effective.
Thank you Matt

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by