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
Geoff Hayes
2016년 5월 15일
Mila - what are the dimensions for alpha, popul, u, and C?
답변 (1개)
Walter Roberson
2016년 5월 15일
0 개 추천
"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
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!