How do i make this section of code a callable function, I keep getting errors

1 vue (au cours des 30 derniers jours)
BAILEY MCMASTER
BAILEY MCMASTER le 2 Mai 2023
function [dx,dy,y,x,state]=position(N,i)
for i=1:N
if state(i)==1 % checks if ligand is active and give new position
dx=randi([-360,360],1);
dy=randi([-50,50],1);
x(i)=x(i)+dx;
y(i)=y(i)+dy;
end
end
x=mod(x,L);
y=mod(y,L);

Réponses (1)

Walter Roberson
Walter Roberson le 2 Mai 2023
What is the point of passing in i as the second parameter, if in the very first line you are going to overwrite i because of the for i loop ?
if state(i)==1 % checks if ligand is active and give new position
You never assign to state and state is not passed in. We can tell from the fact there is no end matching the function that this is not a context in which you might potentially be sharing a variable. So for the state(i) part to work, state would have to be a function that accepts at least one input. But if it is a function, then why are you trying to output state as the 5th output ?
  3 commentaires
BAILEY MCMASTER
BAILEY MCMASTER le 3 Mai 2023
also state is a vector with values of 1 and 2
Walter Roberson
Walter Roberson le 3 Mai 2023
Maybe you should be passing state into the function instead of passing the useless i into the function.
However, if you are never assigning to state inside the function, it is not obvious why you would want to return it as output -- unless you were operating inside a context that expects to pass state and expects that you might have changed the state, such as can happen if you are using a outputfcn or plotfcn callback for one of the optimizers such as ga()

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming 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!

Translated by