Skipping certain values while using a loop
Afficher commentaires plus anciens
Hi, so I am writing a piece of code to loop to find the max in a data set. I need to find the max for every year in a 30 year period. (all the data is in an excel spreadsheet)
My program half works, but the problem I have is the way the data is arranged in the spreadsheet a year, for example, 1987 is in there multiple times since the datasheet has values for every minute. My loop loops through every minute to find the average, which works fine, but when I increment it up, it repeats the same year. How could I have it skip the same year? I attach my code and a screenshot of the data table for reference.
for i=1:777013
currentYear = years(i);
for j=1:777013
if(years(j) == currentYear)
if(height(j) > currentMax)
currentMax = height(j);
end
else
disp('The height total for')
disp(currentYear)
disp('was')
disp(currentMax)
break
end
end
end

1 commentaire
Mehmed Saad
le 20 Avr 2020
Modifié(e) : Mehmed Saad
le 20 Avr 2020
there are much easier way to solve this problem instead of using nested for loops
Try Logical Indexing
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements 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!