Create a cell array from matrices using for loop

조회 수: 2 (최근 30일)
MarshallSc
MarshallSc 2021년 6월 23일
댓글: MarshallSc 2021년 7월 16일
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.

채택된 답변

Stephen23
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)
out = 10×10 cell array
{4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double}
Compare:
out{1}
ans = 4×4
0.9472 2.1173 1.7208 0.7524 0.2632 0.5883 0.4782 0.2091 0.6565 1.4676 1.1927 0.5215 0.3798 0.8490 0.6900 0.3017
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)])
ans = 4×4
0.9472 2.1173 1.7208 0.7524 0.2632 0.5883 0.4782 0.2091 0.6565 1.4676 1.1927 0.5215 0.3798 0.8490 0.6900 0.3017
  댓글 수: 10
MarshallSc
MarshallSc 2021년 7월 15일
So sorry Stephen, I just realized what a silly mistake I made! I figured it out! Thank you brother!
MarshallSc
MarshallSc 2021년 7월 16일
Stephen, I don't know if it's allowed to say it here, but I just wanted to thank you for all of your kind and invaluable helps with the codes. You personally hepled me out a lot so I wanted to show you my deepes gratitude; your kindness and the time you put into helping others don't go unnoticed man! Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by