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

2 visualizaciones (últimos 30 días)
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.
Can anyone please help me with any suggestions.
Thank you in advance!!
I really appreciate your help.
Kind regards,
Shiv

Respuesta aceptada

Matt J
Matt J el 26 de En. de 2022
Editada: Matt J el 26 de En. de 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)];
  1 comentario
Shiv Karpoor
Shiv Karpoor el 26 de En. de 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.
But any ways your updated answer worked, I just tried it.
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by