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
0 件のコメント
回答 (2 件)
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;
0 件のコメント
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 Center および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!