필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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

채택된 답변

Matt J
Matt J 2022년 1월 26일
편집: Matt J 2022년 1월 26일
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
Shiv Karpoor
Shiv Karpoor 2022년 1월 26일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by