Combine cell array with contant matrices

1 次查看(过去 30 天)
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);

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by