how to run a 3x3 matrix through a for loop and save all instances of the [3x3]
2 views (last 30 days)
Show older comments
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.
2 Comments
Accepted Answer
Adam Danz
on 16 Dec 2019
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 Comments
Adam Danz
on 18 Dec 2019
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
on 18 Dec 2019
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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!