Making slight changes to code- adding loop/returning values
Show older comments
I am given this code table.
1 4 2001 30 29 8 1
2 4 2001 4 5 2 1
2 4 2001 9 6 4 7
2 4 2002 19 14 7 3
2 4 2003 25 21 5 4
2 4 2004 7 18 4 5
2 4 2005 8 3 4 10
2 4 2005 10 27 8 0
2 4 2005 12 22 5 6
and I created this code that gives the average of the 6th and 7th column.
% Yearstart=mean(sum(tab(tab(:,3)==firstyear,6:7), 2));
% Yearstart=mean(sum(tab(tab(:,3)==firstyear+1,6:7), 2));
Year2001=mean(sum(tab(tab(:,3)==2001,6:7), 2));
Year2002=mean(sum(tab(tab(:,3)==2002,6:7), 2));
Year2003=mean(sum(tab(tab(:,3)==2003,6:7), 2));
Year2004=mean(sum(tab(tab(:,3)==2004,6:7), 2));
Year2005=mean(sum(tab(tab(:,3)==2005,6:7), 2));
%Yearend=mean(sum(tab(tab(:,3)==endyear-1,6:7), 2));
%Yearend=mean(sum(tab(tab(:,3)==endyear,6:7), 2));
AverageForAllYears= [Yearstart,Year2001,Year2002,Year2003,Year2004,Year2005, Yearend];
HighestAverageYear= max(AverageForAllYears); %this gives the highest average year
I would like help on 2 things, when I am given a different table with different Years I would like to have my code work for those years say earliest year was 1995 and the latest was 2010 my code would not work. I would like a code that finds the average for each year from the earliest value in the table to the latest (I tried include an example of what I'm kind of looking for in the code commented). And finally if possible, how would I return that year in an answer. For example say max(AverageForAllYears) produces a value of 9.5, and lets say 9.5 occured in the year 2002. How would I produce a final answer of 2002. Thanks for the help.
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!