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 Comments

@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.

Sign in to comment.

 Accepted Answer

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 Comment

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.

Sign in to comment.

More Answers (0)

Categories

Find more on Debugging and Improving Code in Help Center and File Exchange

Asked:

AJT
on 7 Aug 2017

Commented:

AJT
on 7 Aug 2017

Community Treasure Hunt

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

Start Hunting!