Matrix dimensions must agree

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

Walter Roberson
Walter Roberson on 8 May 2016
x(:,i) is a column.
mbest is sum(pbest)/popsize and pbest = x and x is two dimensional. sum() of a two dimensional array will give you a row vector, so mbest is going to be a row vector.
You have mbest-x(:,i) so that is a row vector minus a column vector. That is going to fail.

4 Comments

Thank you for your answer, but how I can corrected this error? Because the formula of mbest is fixed.
If the formula for mbest is fixed then you need to rewrite everything else to work by row.
Have you considered the possibility of having mbest be the sum along rows?
Watch out by the way to be sure that u will match the shape of the result
C formulas is fixed and the popul formula also
I try everything changed but I have nothing to do. this is the only error of the program.
for i=1:PopSize,
A(:,i)=bestpos(:,g);
end
R1=rand(dim,PopSize);
R2=rand(dim,PopSize);
C=sum(bestpos)/PopSize;
alpha=(1.0-0.5)*(MaxIt-iter)/MaxIt+0.5;
for i=1:PopSize,
fi=c1*R1/(c1*R1+c2*R2);
p=fi*bestpos(i)+(1-fi)*fbestpart;
u=rand(dim,PopSize);
popul(:,i)=p+((-1).^ceil(0.5+fi)).*(alpha.*abs(popul(:,i)C).*log(1./u));
fpopul(i)=feval(f,popul(:,i));
fevals=fevals+1;
end
the error matrix size is in this line:
popul(:,i)=p+((-1).^ceil(0.5+fi)).*(alpha.*abs(popul(i)-C).*log(1./u));
Please post your current code. The code you have posted so far is missing at least one "end", and the line
popul(:,i)=p+((-1).^ceil(0.5+fi)).*(alpha.*abs(popul(:,i)C).*log(1./u));
is missing a "-" before the "C", indicating that you did not post your current code.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!