How do i make this section of code a callable function, I keep getting errors
2 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
回答(1 个)
Walter Roberson
2023-5-2
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 个评论
Walter Roberson
2023-5-3
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()
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!