Info

此问题已关闭。 请重新打开它进行编辑或回答。

Matrix dimensions must agree

1 次查看(过去 30 天)
mila boub
mila boub 2016-5-15
关闭: MATLAB Answer Bot 2021-8-20
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 个评论
Geoff Hayes
Geoff Hayes 2016-5-15
Mila - what are the dimensions for alpha, popul, u, and C?
mila boub
mila boub 2016-5-15
编辑:mila boub 2016-5-15
popul 2*30
alpha=0.75
u 2*30
C 1*30

回答(1 个)

Walter Roberson
Walter Roberson 2016-5-15
"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?

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by