Making slight changes to code- adding loop/returning values
1 view (last 30 days)
Show older comments
James Connor
on 21 Oct 2015
Commented: Star Strider
on 21 Oct 2015
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.
0 Comments
Accepted Answer
Star Strider
on 21 Oct 2015
This code does its best to be robust to the years:
tab = [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];
tab3 = tab(:,3)-min(tab(:,3))+1; % Create Normalised Date Range Vector
mean6 = accumarray([tab3], [tab(:,6)], [], @mean); % Mean Column 6
mean7 = accumarray([tab3], [tab(:,7)], [], @mean); % Mean Column 7
Output = [[min(tab(:,3)):max(tab(:,3))]' mean6 mean7]; % Result
1 Comment
Star Strider
on 21 Oct 2015
I don’t understand ‘missed a minus between mean6 and mean7’. I didn’t see any minuses, and I didn’t see that you wanted the difference between the columns.
‘Is there anyway I can return the output to the corresponding year?’
The ‘Output’ array has the years in the first column and the corresponding means for Columns 6 and 7 in the second and third columns, respectively. When I looked at them and did some sample calculations to verify my code, they were correct.
I didn’t calculate ‘AverageForAllYears’ (it seems the first and last rows are duplicated) and ‘HighestAverageYear’ because I didn’t understand how you created and calculated them. The max function will take the maximum over the columns (its default behaviour), and with two arguments, will return the index of the first occurrence of the maximum in the column. If you do this:
[outmax,idx] = max(Output(:,2:3));
the ‘idx’ output will return [2 5] because different maxima for the columns occur in different years. The years would be:
HighestAverageYear = Output(idx,1)
those being [2002; 2005] with these data.
More Answers (0)
See Also
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!