Info
This question is closed. Reopen it to edit or answer.
Matrix dimensions must agree
1 view (last 30 days)
Show older comments
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
Answers (1)
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?
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!