Azzera filtri
Azzera filtri

Combine cell array with contant matrices

1 visualizzazione (ultimi 30 giorni)
Ming
Ming il 17 Lug 2016
Commentato: Matt J il 18 Lug 2016
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

Risposte (2)

Matt J
Matt J il 17 Lug 2016
Modificato: Matt J il 18 Lug 2016
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 il 18 Lug 2016
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);

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by