Why do I get "array indices must be positive integers or logical values" when running this for loop?
1 view (last 30 days)
Show older comments
Spencer Checkoway
on 29 Jul 2020
Commented: Spencer Checkoway
on 29 Jul 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 Comments
Accepted Answer
Sriram Tadavarty
on 29 Jul 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
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!