Could I put this in a loop?
信息
此问题已关闭。 请重新打开它进行编辑或回答。
显示 更早的评论
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 个评论
Walter Roberson
2019-2-27
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
2019-2-27
Geoff Hayes
2019-2-27
Aren't you using a loop at your previous question https://www.mathworks.com/matlabcentral/answers/447347-fprintf-in-a-loop?
Matthew Covington
2019-2-27
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!