# How do I extract each value from a column matrix and subsitute in a equation?

2 views (last 30 days)
Shiv Karpoor on 26 Jan 2022
Commented: Shiv Karpoor on 26 Jan 2022
Hello MATLAB Community,
I have a small problem with regards to for loop.
I am trying to use the for loop to solve an equation and store all the values from that particular equation, but I am facing a problem where the loop is considering all values of a matrix to solve it.
for example : a1 = (-25 : 25); a2 = (-25 : 25); a3 = (-25 : 25);
Now I use the combvec function the find all possible combinations
comb = (combvec(a1,a2,a3)); and then I say
phi = comb(1,:);
theta = comb(2,:);
psi = comb(3,:);
Now, I need to subsitute each value of phi, theta and psi (all values of column 1 of comb matrix) into the equation:
ROT = [(cos(phi)*cos(psi))-(cos(theta)*sin(phi)*sin(psi)) -(cos(phi)*sin(psi))-(cos(theta)*cos(psi)*sin(phi)) (sin(phi)*sin(theta));...
(cos(psi)*sin(phi))+(cos(phi)*cos(theta)*sin(psi)) (cos(phi)*cos(theta)*cos(psi)-sin(phi)*sin(psi)) -(cos(phi)*sin(theta));...
(sin(theta)*sin(psi)) (cos(psi)*sin(theta)) cos(theta)];
Similarly, for the next iteration I will consider all values of column 2 of comb matrix and so on, upto the last column of the comb matrix.
Kind regards,
Shiv

Matt J on 26 Jan 2022
Edited: Matt J on 26 Jan 2022
Change all of your '*' operators to elementwise operator '.*'
res=@(z) reshape(z,1,1,[]);
phi = res( comb(1,:) );
theta = res( comb(2,:) );
psi = res( comb(3,:) );
ROT = [(cos(phi).*cos(psi))-(cos(theta).*sin(phi).*sin(psi)) -(cos(phi).*sin(psi))-(cos(theta).*cos(psi).*sin(phi)) (sin(phi).*sin(theta));...
(cos(psi).*sin(phi))+(cos(phi).*cos(theta).*sin(psi)) (cos(phi).*cos(theta).*cos(psi)-sin(phi).*sin(psi)) -(cos(phi).*sin(theta));...
(sin(theta).*sin(psi)) (cos(psi).*sin(theta)) cos(theta)];
Shiv Karpoor on 26 Jan 2022
Hi Matt,
I have tried it yesterday, it doesn't give me the answer in the way I want it by changing the operator to elementwise.
This res=@(z) reshape(z,1,1,[]); made a difference. I am getting the output the way I want it in a 3x3 matrix.
Thank you so much, I really Appreciate it.
Kind Regards,
Shiv