How to save data into an array from a for loop
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Star Strider
le 9 Déc 2021
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 commentaires
Star Strider
le 9 Déc 2021
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
.
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!