How to fix Index exceeds matrix dimensions error?
Afficher commentaires plus anciens
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 commentaires
M
le 7 Nov 2017
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).
Youcef Kara
le 8 Nov 2017
Youcef Kara
le 8 Nov 2017
Réponses (1)
the cyclist
le 8 Nov 2017
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).
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!