Matrix dimensions must agree
Show older comments
Hi,I have a problem with this program :
popsize=20;
MAXITER=200;
fevals=0;
dimension=20;
irange_l=-5.12;
irange_r=5.12;
xmax=100;
xmin=0;
M=(xmax-xmin)/2;
alpha=0.75;
runno=2;
f='my_Rastrigin';
iter=0;
data=zeros(runno, MAXITER);
for run=1:runno
x=(irange_r-irange_l)*rand(dimension,popsize,1)+ irange_l;
gbest=zeros(dimension,popsize);
for i=1:popsize
fpopul(i)=feval(f,x(:,i));
fevals=fevals+1;
end
pbest=x;
f_pbest=fpopul;
[ f_gbest,g]=min(fpopul);
while(iter<MAXITER)
iter=iter+1;
for n=1:MAXITER
alpha=(1.0-0.5)*(MAXITER-n)/MAXITER+0.5;
mbest=sum(pbest)/popsize;
for i=1:popsize
fi=rand(1,dimension);
p=fi*pbest(:,i)+(1-fi)*gbest;
u=rand(1,dimension);
x(:,i)=p+((-1).^ceil(0.5+u)).*(alpha.*abs(mbest-x(:,i)).*log(1./u));
x(:,i)=x(:,i)-(xmax+xmin)/2;
x(:,i)=sign(x(:,i)).*min(abs(x(:,i)),M);
x(:,i)=x(:,i)+(xmax+xmin)/2;
fpopul(i)=feval(f,x(:,i));
fevals=fevals+1;
if (fpopul(i)<f_pbest(i))
pbest(:,i)=x(:,i);
f_pbest(i)=fpopul(i);
end
if f_pbest(i)<f_gbest
gbest=pbest(:,i);
f_gbest=f_pbest(i);
end
end
data(run,n)=f_gbest;
end
[f_gbest,g]=min(f_pbest)
the peobleme is the dimension of matrix but I can not the corrected.
x(:,i)=p+((-1).^ceil(0.5+u)).*(alpha.*abs(mbest-x(:,i)).*log(1./u));
please help me.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!