How to fix Index exceeds matrix dimensions error?

clear,clc
Ts=input('What is T? ');
Vs=input('What is V? ');
WCFs=wcf(Ts,Vs);
fprintf('The Wind Chill Factor (WCF) is %.2f \n',WCFs);
YesNo=input('Do you want the table? ','s');
if strcmpi(YesNo,'Yes')==1
T=-20:5:55;
V=0:5:55;
for j=1:length(T)
WCFg(j)=wcf(T(j),V(j));
end
fprintf('----------------------------------------------\n')
fprintf(' Tempererature(F) Wind speed(m/s) WCF\n')
fprintf('----------------------------------------------\n')
for i=1:length(T)
fprintf(' %.2f %.2f %.2f\n',T(i),V(i),WCFg(i))
end
fprintf('-----------------------------------------------\n')
end
function WCF=wcf(T,V)
WCF=35.7+0.6*T-35.7*(V^0.16)+0.43*T*(V^0.16);
end

3 Comments

What is exactly the error you get?
When I run your code, I find that the size of V does not match the size of T and you have a problem wen trying to acces V(j) with j=1:length(T).
The problem is at the line 12:
WCFg(j)=wcf(T(j),V(j));
Ohh you're right, that was my problem. Thank you very much !!

Sign in to comment.

Answers (1)

Expanding on the above comment by "M":
Your T and V vectors are defined as
T=-20:5:55;
V=0:5:55;
Notice that V has fewer element than T does. So when you get to the loop
for j=1:length(T)
WCFg(j)=wcf(T(j),V(j));
....
end
your looping variable j will get larger than the number of elements in V, and therefore that index exceeds the dimension of V when it tries to execute V(j).

Tags

Asked:

on 7 Nov 2017

Answered:

on 8 Nov 2017

Community Treasure Hunt

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

Start Hunting!