Filter löschen
Filter löschen

Subscript indices must either be real positive integers or logicals?

1 Ansicht (letzte 30 Tage)
The below code is not able to execute due to the stated error in the line "U(i,:)=max(lb,U(i,:))" , but I really cant find any error.
tic
%%prob settings
lb=[-100 -100 ];
ub= [100 100 ];
%% parameters for differential evolution
np=10;
max=500;
%it=100;
cr=0.7;
F=0.8;
e=exp(1);
%x_ss=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
%% START of DE
f=NaN(np,1);
fu=NaN(np,1);
d=length(lb);
U=NaN(np,d);
p= repmat(lb,np,1)+(repmat((ub-lb),np,1).*rand(np,d));
for i=1:np
x=p(i,1);
y=p(i,2);
f(i)= -(20*exp(-0.2*sqrt((1/2)*(x^2+y^2))))+20+e-exp((1/2)*(cos(2*pi*x)+cos(2*pi*y)));
end
%% iteration loop
for it=1:max
for i=1:np
candidates=[1:i-1 i+1:np];
idx= candidates(randperm(np-1,3));
X1= p(idx(1),:);
X2= p(idx(2),:);
X3= p(idx(3),:);
v= X1+F*(X2-X3);
for j=1:d
irand= randi(d,1);
if (rand<=cr || j==irand)
U(i,j)= v(j);
else
U(i,j)=p(i,j);
end
end
end
%% fitness function
for i=1:np
U(i,:)= min(ub,U(i,:));
U(i,:)= max(lb,U(i,:));
A=U(i,:);
x=A(1);
y=A(2);
fu(i)= -(20*exp(-0.2*sqrt((1/2)*(x^2+y^2))))+20+e-exp((1/2)*(cos(2*pi*x)+cos(2*pi*y)));
A=p(i,:);
x=A(1);
y=A(2);
f(i)= -(20*exp(-0.2*sqrt((1/2)*(x^2+y^2))))+20+e-exp((1/2)*(cos(2*pi*x)+cos(2*pi*y)));
if (fu(i)<f(i))
p(i,:)= U(i,:);
f(i)=fu(i);
else
p(i,:)=p(i,:);
end
end
end
toc
[bestfitness ind]= min(f)
bestsol=p(ind,:)

Akzeptierte Antwort

Chunru
Chunru am 29 Mai 2021
You define max as a variable
max=500;
However you intend to use max as a function in:
U(i,:)= max(lb,U(i,:));
Since max is defined as a variable (overwriting as a function), the above statement is trying to index to max as a scalar variable. This causes the error.
To correnct the error, change max (as a variable) into other names, for example:
NMax = 500;
You may also need to change the following line accordingly:
for it=1:max % max==>NMax

Weitere Antworten (1)

Image Analyst
Image Analyst am 29 Mai 2021

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by