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

1 回表示 (過去 30 日間)
BAILEY MCMASTER
BAILEY MCMASTER 2023 年 5 月 2 日
コメント済み: Walter Roberson 2023 年 5 月 3 日
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);

回答 (1 件)

Walter Roberson
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 件のコメント
BAILEY MCMASTER
BAILEY MCMASTER 2023 年 5 月 3 日
also state is a vector with values of 1 and 2
Walter Roberson
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()

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by