Matrix dimensions must agree

Hi, I have a problem with this program.
for i=1:PopSize,
A(:,i)=bestpos(:,g);
end
R1=rand(dim,PopSize);
R2=rand(dim,PopSize);
vel=w*vel+c1*R1.*(bestpos-popul)+c2*R2.*(A-popul);
fi=c1*R1/(c1*R1+c2*R2);
for i=1:PopSize,
C=sum(bestpos)./PopSize;
p=fi*bestpos(i)+(1-fi)*fbestpart;
u=rand(dim,PopSize);
popul(i)=p+(alpha.*abs(popul(i)-C).*log(1./u));
end
the error is : Error using .* Matrix dimensions must agree.
Error in (line 58)
popul(i)=p+(alpha.*abs(popul(i)-C).*log(1./u));
The formula C and Popul are fair and fixed. Please help me to solve this error.

2 Comments

Mila - what are the dimensions for alpha, popul, u, and C?
mila boub
mila boub on 15 May 2016
Edited: mila boub on 15 May 2016
popul 2*30
alpha=0.75
u 2*30
C 1*30

Answers (1)

Walter Roberson
Walter Roberson on 15 May 2016
"The formula C and Popul are fair and fixed"
It is not possible to fix the code under that constraint, just as it was not possible in your previous posting of the same question, http://www.mathworks.com/matlabcentral/answers/283172-matrix-dimensions-must-agree
Your C is 1 x 30. abs(popul(i)-C) is therefore going to be 1 x 30. Your u is 2 x 30, so log(1./u) is gong to be 2 x 30. p and alpha are constants so they do not change the shape of the calculation.
You now have to have an operation between a 1 x 30 object and a 2 x 30 object. The possible legal output sizes for such an operation are 1 x 2 or 2 x 1. But you are assigning to popul(i) which is a scalar, so you need the outcome to be 1 x 1. You are stuck.
Did you notice, by the way, that your popul is 2 x 30 but you are accessing it as if it is a scalar?

This question is closed.

Tags

Asked:

on 15 May 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!