Finding averages in an array

3 ビュー (過去 30 日間)
Max
Max 2015 年 10 月 15 日
回答済み: Jos (10584) 2015 年 10 月 16 日
1 4 2001 30 29 8 1
2 4 2002 4 5 2 1
2 4 2002 9 6 4 7
2 4 2002 19 14 7 3
2 4 2002 25 21 5 4
2 4 2002 7 18 4 5
2 4 2002 8 3 4 10
2 4 2003 10 27 8 0
Each row represents a match played. Column 6 represents the goals scored by 1 team and column 7 represents the goals scored by the opposition team. How do I find the average number of goals scored per match in the year 2002 (column 3)
Thanks for the help.

採用された回答

Thorsten
Thorsten 2015 年 10 月 15 日
編集済み: Thorsten 2015 年 10 月 15 日
To find the average across all matches
mean(sum(X(:,6:7), 2))
  4 件のコメント
Max
Max 2015 年 10 月 15 日
So how would I write the final answer for average score per game for the year 2002.
Sorry I'm quite new to matlab.
Nobel Mondal
Nobel Mondal 2015 年 10 月 16 日
avgScoreFor2002 = mean(sum(X(X(:,3)==2002,6:7), 2));
sprintf('Average score/match for 2002 : %.4f', avgScoreFor2002)

サインインしてコメントする。

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2015 年 10 月 16 日
As a beginner, you're better of to write out each individual step of the process in detail
Is2002Game = X(:,3)==2002 % true when the 3rd column equals 2002
Goals2002 = X(Is2002Game,[6 7]) % select goals of these matched
TotalScorePer2002Game = sum(Goals2002,2) % sum them per match
TotalScore = sum(TotalScorePer2002Game) % total sum

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by