Create a cell array from matrices using for loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
MarshallSc
am 23 Jun. 2021
Kommentiert: MarshallSc
am 16 Jul. 2021
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 Kommentare
Akzeptierte Antwort
Stephen23
am 23 Jun. 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)
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)])
10 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!