Could I put this in a loop?
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
This is the following code I have
% PART C
% use find() and length() to determine how many months of each year exceed overall average
year2013 = find(lake_powell(:,1) > overall_average);
year2014 = find(lake_powell(:,2) > overall_average);
year2015 = find(lake_powell(:,3) > overall_average);
year2016 = find(lake_powell(:,4) > overall_average);
months2013 = length(year2013);
months2014 = length(year2014);
months2015 = length(year2015);
months2016 = length(year2016);
% print part C
fprintf('\nPART C: Determine how many months of each year > overall average')
fprintf('\n During 2013 the lake was above average for %2.0f months', months2013)
fprintf('\n During 2014 the lake was above average for %2.0f months', months2014)
fprintf('\n During 2015 the lake was above average for %2.0f months', months2015)
fprintf('\n During 2016 the lake was above average for %2.0f months\n', months2016)
This is the correct output, however is there a simpler way to do this and get the same output? My professor says I can only use the length and find functions to get the values.
PART C: Determine how many months of each year > overall average
During 2013 the lake was above average for 1 months
During 2014 the lake was above average for 7 months
During 2015 the lake was above average for 9 months
During 2016 the lake was above average for 12 months
5 commentaires
Walter Roberson
le 27 Fév 2019
You could add a for loop and use indexing instead of named variables, but otherwise "not really", not under those restrictions.
A simpler way of doing the counting would be
sum(lake_powell(:,1:4) > overall_average)
Matthew Covington
le 27 Fév 2019
Geoff Hayes
le 27 Fév 2019
Aren't you using a loop at your previous question https://www.mathworks.com/matlabcentral/answers/447347-fprintf-in-a-loop?
Matthew Covington
le 27 Fév 2019
Walter Roberson
le 27 Fév 2019
Use years(n) in your fprintf()
Réponses (0)
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!