Finding how many times a team lost from an array

1 view (last 30 days)
Im given
1 5 2004 16 8 13 3
1 5 2004 21 12 8 6
1 5 2005 22 25 3 8
1 5 2005 23 22 2 1
1 5 2005 22 19 3 6
2 5 2005 20 29 2 6
2 5 2005 20 5 1 3
2 5 2005 20 28 2 3
2 5 2005 27 18 2 6
2 5 2005 2 25 8 9
2 5 2006 8 26 9 10
2 5 2006 24 1 0 5
column 4 represents the home team, and column 5 represents the away team. Column 6 represents the home team goals and column 7 represents the away teams goals. How do i write a piece of code that shows how many teams lost at most 3 times playing in the year 2005. From the table you can see team 22 and team 20 lost at most 3 times in 2005 so I would like my answer to be 2.
So column 4 represents the home team and column 5 represents the away team column 6 represents the home team goals and column 7 represents the away team goals. Ignoring the first 2 columns row 1 would mean that in 2004 team 16 beat team 8 with a score of 13-3. I would like to find out for the year 2005 what teams lost at most 3 times.
  2 Comments
Star Strider
Star Strider on 11 Oct 2015
Please clarify the column interpretations. Your description leaves much to the imagination.
Max
Max on 11 Oct 2015
So column 4 represents the home team and column 5 represents the away team column 6 represents the home team goals and column 7 represents the away team goals. Ignoring the first 2 columns row 1 would mean that in 2004 team 16 beat team 8 with a score of 13-3. I would like to find out for the year 2005 what teams lost at most 3 times.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 11 Oct 2015
Try this:
m = [...
1 5 2004 16 8 13 3
1 5 2004 21 12 8 6
1 5 2005 22 25 3 8
1 5 2005 23 22 2 1
1 5 2005 22 19 3 6
2 5 2005 20 29 2 6
2 5 2005 20 5 1 3
2 5 2005 20 28 2 3
2 5 2005 27 18 2 6
2 5 2005 2 25 8 9
2 5 2006 8 26 9 10
2 5 2006 24 1 0 5]
year2005Rows = m(:, 3) == 2005
homeTeams = m(year2005Rows, 4)
homeTeamGoals = m(year2005Rows, 6)
awayTeamGoals = m(year2005Rows, 7)
homeTeamWins = homeTeamGoals > awayTeamGoals
winsByTeam = grpstats(homeTeamWins, homeTeams, 'max')
teamsInOrder = unique(homeTeams)
for t = 1 : length(winsByTeam)
fprintf('Team %d won %d times at home in 2005.\n', ...
teamsInOrder(t), winsByTeam(t));
end
In the command window:
Team 2 won 0 times at home in 2005.
Team 20 won 0 times at home in 2005.
Team 22 won 0 times at home in 2005.
Team 23 won 1 times at home in 2005.
Team 27 won 0 times at home in 2005.
It requires grpstats from the Statistics and Machine Learning toolbox.

More Answers (0)

Categories

Find more on Dates and Time 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!