필터 지우기
필터 지우기

Combine cell array with contant matrices

조회 수: 1 (최근 30일)
Ming
Ming 2016년 7월 17일
댓글: Matt J 2016년 7월 18일
Hi everyone,
Suppose I have a cell array A{1} A{2} A{3} ... A{n}, can I create a new cell array A_{1} A_{2} A_{3} ... A_{n}, such that A_{1}=[0 0 0;0 A{1} 0;0 0 0], A_{2}=[0 0 0;0 A{2} 0;0 0 0], A_{n}=[0 0 0;0 A{n} 0;0 0 0], without loop?
Is there any efficient way to perform this operation?
P.S. A{1:n} are all same size matrices, 0 represents a zero matrix.
thanks

답변 (2개)

Matt J
Matt J 2016년 7월 17일
편집: Matt J 2016년 7월 18일
No, there is no way to avoid loops when working with cell arrays (including those hidden in cellfun). However, if you are storing things in cell arrays, it probably means your data isn't large enough to make efficiency a very big concern.
If A{1:n} are all same size matrices, it would have served you better to store them as a 3D array,
A3d=cat(3,A{:});
You could then create a similar, zero-padded 3d array by doing,
A3d_=zeros(...);
A3d_(i:j,m:n,:)=A3d;

Andrei Bobrov
Andrei Bobrov 2016년 7월 18일
A1 = cat,3(A{:});
A2 = zeros(size(A1)+[2,2,0]);
A2(2:end-1,2:end-1,:) = A1;
A_ = reshape(num2cell(A2,[1,2]),[],1);
  댓글 수: 1
Matt J
Matt J 2016년 7월 18일
num2cell has hidden for loops in it...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by