How to represent a set of matrices into a single cell?

1 回表示 (過去 30 日間)
SM
SM 2020 年 7 月 13 日
コメント済み: SM 2020 年 7 月 13 日
My problem is as follows:
a=4;
b=4;
A=round((30-10).*rand(a,b) + 10);
B=round((100-40).*rand(a,b) + 40);
C=round((250-80).*rand(a,b) + 80);
D=round((80-20).*rand(a,b) + 20);
E=cell(a,b);
for i=1:a
for j=1:b
E{i,j}=[A(i,j) B(i,j) C(i,j) D(i,j)];
end
end
Is there any simple and short ways instead of using loop or any other unnecessary lines?

採用された回答

madhan ravi
madhan ravi 2020 年 7 月 13 日
E = num2cell([A(:), B(:), C(:), D(:)], 2);
E = reshape(E, a, b)

その他の回答 (1 件)

David Hill
David Hill 2020 年 7 月 13 日
I would keep them all in a single matrix and then form matrix E (a*b X 4) where each row would hold the cell information of your orginal code.
a=4;b=4;c=[30,100,250,80];d=[10,40,80,20];
for k=1:4
A(:,:,k)=round((c(1)-d(1))*rand(a,b)+d(1));
end
B=permute(A,[2,1,3]);
E=reshape(B(:),a*b,[]);

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by