Why is the message for my program Error using + Matrix dimensions must agree.??

Vf=zeros(numel(rn),numel(rn));
for c=1:numel(rn)
for b=1:Lc
Tnf(c)=(1/sqrt(rn(c).^2+(G+1-zn(c)).^2))-(1/sqrt(rn(c).^2+(G+1+zn(c)).^2));
W1nf(c,b)=log((Aj(b)+zn(c)+sqrt(rn(c).^2+(Aj(b)+zn(c)).^2))/(Aj(b)-zn(c)+sqrt(rn(c).^2+(Aj(b)-zn(c)).^2)));
Pijf= [W1nf,Tnf'];
Vf=Vf+Pijf.*Q1ij(j);
end
end
I have checked the size of the variables. they are matching.why this is showing error using plus?? Whether i have to set Vf to same dimensions..(j and rn is defined). Vf,Pijf,W1nf,Tnf are arrays...

3 个评论

@AJT: You use + twelve times. Do you want us to guess which one throws the error?
And how did you check? Did you use the debugger? Things are often not what you think they are, and the error message is pretty straightforward.

请先登录,再进行评论。

 采纳的回答

If you did not pre-allocate W1nf and Tnf before the loop, these array grow in each iteration. Then Pijf is a [c, b+1] matrix. Adding this to the [numel(rn), numel(rn)] matrix Vf must fail:
Pijf= [W1nf,Tnf'];
Vf=Vf+Pijf.*Q1ij(j); % <-- Cannot work
It is easy to check this using the debugger. Type this in the command window:
dbstop if error
and run the code again. When it stops, check the sizes of the locally used variables.
When you discuss the problem in the forum, care for posting the complete error message, such that the readers do not have to guess in which line the error occurres.
By the way: Mov the calculation of Tnf out of the inner loop, because it does not depend on b

1 个评论

Yes Simon. your answer is right. Vf dimensions must be matched. Also i have used debugger and its showing the correct line where we have suspected.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Debugging and Improving Code 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by