Effacer les filtres
Effacer les filtres

Place each dimension of matrix into a cell array

1 vue (au cours des 30 derniers jours)
Fernando Duarte
Fernando Duarte le 5 Avr 2011
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)

Réponse acceptée

Matt Fig
Matt Fig le 5 Avr 2011
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 commentaire
Fernando Duarte
Fernando Duarte le 5 Avr 2011
Great answer, short and effective.
Thank you Matt

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by