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

1 次查看(过去 30 天)
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

采纳的回答

Sriram Tadavarty
Sriram Tadavarty 2020-7-29
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by