How to save data into an array from a for loop
1 回表示 (過去 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
0 件のコメント
回答 (1 件)
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 件のコメント
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 Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!