필터 지우기
필터 지우기

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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by