Create a cell array from matrices using for loop

4 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Stephen23
Stephen23 il 23 Giu 2021
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 Commenti
MarshallSc
MarshallSc il 15 Lug 2021
So sorry Stephen, I just realized what a silly mistake I made! I figured it out! Thank you brother!
MarshallSc
MarshallSc il 16 Lug 2021
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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by