Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Could I put this in a loop?

4 ビュー (過去 30 日間)
Matthew Covington
Matthew Covington 2019 年 2 月 27 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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 件のコメント
Matthew Covington
Matthew Covington 2019 年 2 月 27 日
This is what I had in my code at first then I wanted to make it shorter and cleaner so I tried to make it into a loop. However I could not figure out how to match up the code where it will display the correct output with the restrictions I am given.
Walter Roberson
Walter Roberson 2019 年 2 月 27 日
Use years(n) in your fprintf()

回答 (0 件)

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by