Effacer les filtres
Effacer les filtres

Trouble using while loop

2 vues (au cours des 30 derniers jours)
Alexander Nicholas
Alexander Nicholas le 17 Avr 2017
Réponse apportée : Nick le 17 Avr 2017
I am trying to get the number of times the value tempv is 50 or higher, as well as the average of tempv, both using a while loop. I entered the below
clear, clc
tempv = [50, 60, 65, 30, 40, 66, 30, 55, 72, 72, 75, 50, 60, 65, 30, 40, ...
66, 30, 55, 63, 72, 75, 50, 60, 65, 30, 40, 66, 30, 55, 53];
% Write your code here:
n=0;
x=length(tempv);
sum=0;
i=0;
while i<31
if tempv(i) >= 50 && tempv(i) <= 75
n=n+1;
fprintf('It was a warm day on May %i\n', i)
end
sum=sum+tempv(i);
end
fprintf('In May the number of days the T was over 50 was: %i\n', n)
y=sum/x;
fprintf('The average temperature in Fahrenheit was: %f\n', y)
However this returns the following error in the command window:
Subscript indices must either be real positive integers or logicals.
What is the error referring to here? Where is my mistake?

Réponses (1)

Nick
Nick le 17 Avr 2017
You are setting i = 0 and then trying to access tempv[0]. In matlab indexing starts with 1 not 0.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by