Create a cell array from matrices using for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello, I'm looking to derive a 10*10 cell array that each cell contains a 4*4 matrix within each cell. For example, by having four 10*10 matrices, I want to derive a 4*4 matrix for each cell:
x1=rand(10,10);
y1=rand(10,10);
z1=rand(10,10);
r1=rand(10,10);
x2=rand(10,10);
y2=rand(10,10);
z2=rand(10,10);
r2=rand(10,10);
I want to create a cell like:
L=cell(10,10);
In which I want to insert the sqrt of the ratio of the 4 matrices in each cell like:
L{1,1}=sqrt([x1(1,1)/x2(1,1), (x1(1,1)/y2(1,1)), x1(1,1)/z2(1,1), x1(1,1)/r2(1,1);...
y1(1,1)/x2(1,1), y1(1,1)/y2(1,1), y1(1,1)/z2(1,1), y1(1,1)/r2(1,1);...
z1(1,1)/x2(1,1), z1(1,1)/y2(1,1), z1(1,1)/z2(1,1), z1(1,1)/r2(1,1);...
r1(1,1)/x2(1,1), r1(1,1)/y2(1,1), r1(1,1)/z2(1,1), r1(1,1)/r2(1,1)])
So for each cell I need to calculate the cell arrays (L{1,2}, L{1,3} . . . L{10,10})
I want to use the for loop that can do the same procedure for all the cell array that each contain a 4*4 matrix that at the end I have 10*10 cell that each cell contain a 4*4 matrix. But my for loop doesn't give me the answer. Can someone please help me with writing the for loop? Thank you.
댓글 수: 0
채택된 답변
Stephen23
2021년 6월 23일
x1 = rand(10,10);
y1 = rand(10,10);
z1 = rand(10,10);
r1 = rand(10,10);
a1 = cat(3,x1,y1,z1,r1);
x2 = rand(10,10);
y2 = rand(10,10);
z2 = rand(10,10);
r2 = rand(10,10);
a2 = cat(4,x2,y2,z2,r2);
tmp = num2cell(sqrt(a1./a2),3:4);
fun = @(a)reshape(a,4,4);
out = cellfun(fun,tmp,'uni',0)
Compare:
out{1}
sqrt([x1(1,1)/x2(1,1), (x1(1,1)/y2(1,1)), x1(1,1)/z2(1,1), x1(1,1)/r2(1,1);...
y1(1,1)/x2(1,1), y1(1,1)/y2(1,1), y1(1,1)/z2(1,1), y1(1,1)/r2(1,1);...
z1(1,1)/x2(1,1), z1(1,1)/y2(1,1), z1(1,1)/z2(1,1), z1(1,1)/r2(1,1);...
r1(1,1)/x2(1,1), r1(1,1)/y2(1,1), r1(1,1)/z2(1,1), r1(1,1)/r2(1,1)])
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!