Why do I get "array indices must be positive integers or logical values" when running this for loop?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Spencer Checkoway
le 29 Juil 2020
Commenté : Spencer Checkoway
le 29 Juil 2020
i_theta_max =100;
for i_theta = 0:i_theta_max
theta = i_theta/(i_theta_max)*2*pi;
xcoord(i_theta)=1*sin(theta);
ycoord(i_theta)=1*cos(theta);
end
0 commentaires
Réponse acceptée
Sriram Tadavarty
le 29 Juil 2020
Hi Spencer,
The access of i_theta in xcoord and ycoord is the issue. In MATLAB, indexing is one based.
Try to update as folllowing:
i_theta_max =100;
for i_theta = 0:i_theta_max
theta = i_theta/(i_theta_max)*2*pi;
xcoord(i_theta+1)=1*sin(theta); % Added 1
ycoord(i_theta+1)=1*cos(theta); % Added 1
end
% or
i_theta_max =100;
for i_theta = 1:i_theta_max+1
theta = (i_theta-1)/(i_theta_max)*2*pi; % Subtract 1
xcoord(i_theta)=1*sin(theta);
ycoord(i_theta)=1*cos(theta);
end
Hope this helps.
Regards,
Sriram
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!