Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

조회 수: 2 (최근 30일)
I am trying to run this code but it gives me a lot of error.
function vi=successor(optimValues, ~)
%get current state, which is automatically put in optimValues
xi = optimValues.x;
nVar = length(xi);
lb=-5.12;
ub=5.12;
%Choose a random solution xk. Next solution will be shifted towards xk in the neighbourhood of xi
xk=GenerateRandomSolution(nVar,lb,ub);
% choose a random dimension j to vector xk = the next solution vi will only be on dimension j direction in the neighbourhood
j= randi([1 nVar]);
% define neighbourhood coefficient and choose a random coefficient in neighbourhood size [-1 1]
q=1; % Neighbourhood diameter upper bound
phi=q*unifrnd(-1,+1);
% generate a new successor vi
vi=xi;
vi(j)=xi(j)+phi.*(xi(j)-xk(j));
if vi(j)>ub
vi(j)=ub;
elseif vi(j)<lb
vi(j)=lb;
end
end
and Error code is below:
>> successor(optimValues, ~)
successor(optimValues, ~)
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
Please tell me how I can get past this.
thanks
  댓글 수: 2
Gizen Mutlu
Gizen Mutlu 2019년 10월 27일
When I tried
successor(optimValues)
I get this error;
Not enough input arguments.
Error in successor (line 4)
xi = optimValues.x;
but is curret state getting automatically in optimValues?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2019년 11월 1일
You would get the error about not enough input arguments on the line
xi = optimValues.x;
under the circumstance that the optimValues you passed in is a function handle that needs at least one argument, and you are running R2019b or later.

Siriniharika Katukam
Siriniharika Katukam 2019년 11월 1일
Hi
Since you are using optimValues.x, optimValues must support dot indexing. The error in line 4 usually arise when optimValues.x is not initialized in optimValues. This means, may be your input optimValues is just a scalar.
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 11월 1일
No, if the object does not supprot dot indexing you get
Dot indexing is not supported for variables of this type.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operations Research에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by