Create a cell array from matrices using for loop

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.

 Accepted Answer

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 Comments

Thanks a lot Stephen!
If I want to calculate a set of equations that would be equal to the results that I get, what would be the best method to do that? I want to do the following:
alphaV=-0.5; alphaS=1;
out{1}(1,1)=alphaV*X + (1-alphaV)*Y;
out{1}(1,1)=alphaS*X + (1-alphaS)*Y
That would result in X and Y for each of element of the matrix in the cells.
I want to do this for every element of the matrices in each cell (out{1}(1,2), out{1}(1,3), ...) so that I would have a 10*10 cell array, each cell with 4*4 cells and each cell has 2 values corresponding to the results from the “out”.
Could someone please help me with this part too, I'd really appreciate it.
Thank you.
"If I want to calculate a set of equations that would be equal to the results that I get"
It is not clear how you have "a set of equations" in the mathematical sense, nor how your example makes those equations equal to the "results". By "results" do you mean the numeric content of the cell array out?
"so that I would have a 10*10 cell array, each cell with 4*4 cells..."
That does not match what you requested in your original question: originally you specified that each cell should contain a 4x4 numeric array (which is what my answer gives you, which you accepted).
"...and each cell has 2 values corresponding to the results from the “out”."
Numeric arrays do not have "cells". Cell arrays have cells. Your description is not clear, perhaps an example (using valid MATLAB commands) would be better.
It is not clear why you assign some value to the numeric array element out{1}(1,1), just to replace it on the next line:
out{1}(1,1)=alphaV*X + (1-alphaV)*Y;
out{1}(1,1)=alphaS*X + (1-alphaS)*Y
Thanks Stephen. Yes, I want to do this part after the original part (which you answered and is completely correct), I mean that for example let's suppose the numeric content of out{1}(1,1) is 1.29, I want to equal this value to the equations with two variables, i,e.
1.29=alphaV*X + (1-alphaV)*Y;
1.29=alphaS*X + (1-alphaS)*Y;
And then get a value for X and Y and store them into the out{1}(1,1), so instead of 1.29, now I would have a vector that has two numeric contents instead of one, for example the out{1}(1,1) changes to value of X and Y ([1,2]). In this way for every numeric content of the out, I have two values. Did that make any sense? I think I have to do this separately and store them into a new cell.
I tried to specify the results into the numeric content but matlab gives the error:
Conversion to double from cell is not possible.
So my question is can I do something like that? I don't know if Matlab can do that. If it can't, what would be the best method? Is there any way to do this at all?
Those equations appear to have trivial solutions of X == Y == LHS. If the LHS always consists of the same value twice, then it is not clear what you gain from solving this equation.
alphaV = -0.4;
alphaS = +0.9;
% 1.29=alphaV*X + (1-alphaV)*Y;
% 1.29=alphaS*X + (1-alphaS)*Y;
A = [alphaV,1-alphaV;alphaS,1-alphaS];
B = [1.29;1.29];
x = A\B
x = 2×1
1.2900 1.2900
In any case, probably the easiest approach is to just use a few loops. Preallocate the output nested cell arrays and then loop over them:
out = repmat({cell(4,4)},10,10);
for k1 = 1:numel(out)
for k2 = 1:numel(out{k})
% solve equations here
out{k1}{k2} = A\B;
end
end
Thanks again Stephen. You're right, the X and Y would be equal, but that's not the actual case that I'm dealing with, there's something in the middle of the procedure that I need to do. But thank you for help brother, really appreciate it.
Stephen, when I want to change the inputs for x1,y1,z1 ... into a multiplication of these matrices and use them in the function, the result of some of the "out" gives me a cell with all the components as 1. The cells that give me 1 are out{1,3}, out{2,4}, ou{3,1} and out{4,2}. The only change that I make to the code is just:
b1=x1.*y2;
b2=y1.*y2;
b3=z1.*z2;
b4=r1.*r2;
c1=cat(3,b1,b2,b3,b4);
b5=x1.*z2;
b6=x1.*r2;
b7=x1.*y2;
b8=x1.*x2;
c2=cat(4,b5,b6,b7,b8);
tmp = num2cell(sqrt(c1./c2),3:4);
fun = @(c)reshape(c,4,4);
out = cellfun(fun,tmp,'uni',0);
out= reshape(num2cell(sqrt(c1./c2),1:2),4,4);
But I get some cell arrays value as 1. Do you know why this happens? Thank you!
@MarshallSc: why do you call the three lines of code tmp= ... fun=.. and out=... just to completely ignore their results using the very last line out=... ? What is the point of these three lines that you do not use?
"But I get some cell arrays value as 1. Do you know why this happens?"
n ./ n == 1
So sorry Stephen, I just realized what a silly mistake I made! I figured it out! Thank you brother!
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!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 23 Jun 2021

Commented:

on 16 Jul 2021

Community Treasure Hunt

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

Start Hunting!