how to run a 3x3 matrix through a for loop and save all instances of the [3x3]

5 次查看(过去 30 天)
I have a [3x3] transformation matrix A = [ -sind(omega*t), cosd(omega*t), 0; cosd(omega*t), sind(omega*t) cosd(phi); cosd(omega*t), sind(omega*t), sin(phi)]
omega and phi are constants defined earlier. t is the vaiable that is being looped for. if I have 300 values for t, I would like to output 300 different [3x3] matricies, each one corresponding with a different value of t. for more information t is a value that is being read in from a data sheet.

采纳的回答

Adam Danz
Adam Danz 2019-12-16
A is a cell array the same size as t where A{n} is the matrix for t(n).
omega = 1;
phi = 1;
t = linspace(0,5,300);
A = arrayfun(@(t)[-sind(omega*t), cosd(omega*t), 0;
cosd(omega*t), sind(omega*t) cosd(phi);
cosd(omega*t), sind(omega*t), sin(phi)],t,'UniformOutput',false);
  11 个评论
Adam Danz
Adam Danz 2019-12-18
From your question, "t is the vaiable that is being looped for. if I have 300 values for t".
In reality you have 900 values for t.
If I use these inputs
t = rand(300,3);
A = arrayfun(@(t)[-sind(omega*t), cosd(omega*t), 0;
cosd(omega*t), sind(omega*t) cosd(phi);
cosd(omega*t), sind(omega*t), sin(phi)],t,'UniformOutput',false);
A is a 300x3 cell array. I'm guessing that's not what you want.
It's now unclear how to apply each element of t to the transformation matrix. The current code is processing each single value of t. I suppose you want to input rows of t. Is that correct?
Adam Danz
Adam Danz 2019-12-18
maybe this is what you're looking for
t = rand(300,3);
A = arrayfun(@(i)[-sind(omega*t(i,1)), cosd(omega*t(i,1)), 0;
cosd(omega*t(i,2)), sind(omega*t(i,2)) cosd(phi);
cosd(omega*t(i,3)), sind(omega*t(i,3)), sin(phi)],1:size(t,1),'UniformOutput',false);
or perhaps this
t = rand(300,3);
A = arrayfun(@(i)[-sind(omega*t(i,1)), cosd(omega*t(i,2)), 0;
cosd(omega*t(i,1)), sind(omega*t(i,2)) cosd(phi);
cosd(omega*t(i,1)), sind(omega*t(i,2)), sin(phi)],1:size(t,1),'UniformOutput',false);
But I haven't put too much thought into it, mainly because I gotta run. Think about how the values of t are being fed into the arrayfun and how you want to apply those value to your transformation matrix. I can check back later.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by