Why do I get "array indices must be positive integers or logical values" when running this for loop?

1 view (last 30 days)
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

Accepted Answer

Sriram Tadavarty
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)

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!