How to save data into an array from a for loop

2 次查看(过去 30 天)
This is my code, I need it to run through theta from 1 to 360 degrees, and also run through different values of op for every value of theta. I believe this code does that but I don't know how to save the data from the for loop. Thank you in advance for any advice!
for theta = 1:360
for op = -400:0.5:400
A = [cosd(theta) sind(theta) 0; -sind(theta) cosd(theta) 0; 0 0 1];
Rotstress = A*stress*A.';
oynew = Rotstress(2,2)+op*sind(theta);
oxnew = (Rotstress(1,1))+op*cos(theta);
end
end

回答(1 个)

Star Strider
Star Strider 2021-12-9
It is not possible to use ‘op’ as an index, so it needs to be restated to use it in that context. The ‘theta’ vector can be used as an index without modification here.
opv = -400:0.5:400;
for theta = 1:360
for k = 1:numel(opv)
op = opv(k);
A = [cosd(theta) sind(theta) 0; -sind(theta) cosd(theta) 0; 0 0 1];
Rotstress = A*stress*A.';
oynew(theta,k) = Rotstress(2,2)+op*sind(theta);
oxnew(theta,k) = (Rotstress(1,1))+op*cos(theta);
end
end
I assume the goal is to save ‘oynew’ and ‘oxnew’ so I subscripted them here.
.
  2 个评论
Lucie Jackson
Lucie Jackson 2021-12-9
Thank you! I've actually just realised I made a slight mistake in the code I copied, would it be possible to run it from 0:360 degrees? (when I try this it throws errors as I assume it is not an integer position in the array it is being recorded in)
Star Strider
Star Strider 2021-12-9
No, MATLAB indexing begins at 1, so the code would have to be —
opv = -400:0.5:400;
thetav = 0:360;
for k1 = 1:numel(thetav)
theta = thetav(k1);
for k2 = 1:numel(opv)
op = opv(k);
A = [cosd(theta) sind(theta) 0; -sind(theta) cosd(theta) 0; 0 0 1];
Rotstress = A*stress*A.';
oynew(k1,k2) = Rotstress(2,2)+op*sind(theta);
oxnew(k1,k2) = (Rotstress(1,1))+op*cos(theta);
end
end
.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by