Effacer les filtres
Effacer les filtres

Combine cell array with contant matrices

1 vue (au cours des 30 derniers jours)
Ming
Ming le 17 Juil 2016
Commenté : Matt J le 18 Juil 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

Réponses (2)

Matt J
Matt J le 17 Juil 2016
Modifié(e) : Matt J le 18 Juil 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 le 18 Juil 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);
  1 commentaire
Matt J
Matt J le 18 Juil 2016
num2cell has hidden for loops in it...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by