Mouse Problem: Why do I keep getting the error message, "Array indices must be positive integers or logical values."?
Afficher commentaires plus anciens
I'm not sure why I keep getting the following error messages:
Array indices must be positive integers or logical values.
Error in hehemouse (line 13)
dx=x(mod(i+1,5))-x(i)/sqrt(((x(mod(i+1,5))-x(i))^2)+((y(mod(i+1,5))-y(i))^2));
My code is the following.
% Initialize the positions of the 5 mice
x = [0, -sin(2*pi/5), -sin(4*pi/5), sin(4*pi/5), sin(2*pi/5)];
y = [1, cos(2*pi/5), -cos(pi/5), -cos(pi/5), cos(2*pi/5)];
% For each iteration, each mouse moves a distance d=0.01 towards neighbour ccw
% To find mouse's neighbour, just do (x+1)%5 = generate coordinates
% Repeat iterations until mice are sufficiently close to each other
while ((sqrt((y(2)-y(1)^2))+(x(2)-x(1))^2)>0.01)
for i=1:5
fprintf('%lf %lf', x(i), y(i));
dx=x(mod(i+1,5))-x(i)/sqrt(((x(mod(i+1,5))-x(i))^2)+((y(mod(i+1,5))-y(i))^2));
dy=y(mod(i+1,5))-y(i)/sqrt(((x(mod(i+1,5))-x(i))^2)+((y(mod(i+1,5))-y(i))^2));
x(i)=x(i)+0.01*dx;
y(i)=y(i)+0.01*dy;
end
end
% To graph, store sequence of vectors in matrices with each line representing coordinates at one of the time steps
x = mat(i,:);
y = mat(:,i);
Réponse acceptée
Plus de réponses (1)
Sakz
le 1 Avr 2019
1 vote
The source of error is in : x(mod(i+1,5))
when i=4, then mod(5,5) = 0; then x(0) is causing the error.
Matlab vector index starts from 1.
[ Subscript indices must either be real positive integers or logicals.]
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!