The Student Room Group

Matlab: Question I'm stuck on (Involves a Matrix)

Hi, so basically I have a 2 column matrix which i've called 'A' and about 5,000+ rows. Column 1 is the league name and column 2 is the goals scored (home team and away team combined) for any given match, which is what each row represents - a match.

I need to write a script or function which will generate the league number that has the highest average number of goals scored per match. I'm still quite new to matlab so I have a lack of knowledge when it comes to functions, and I have little idea how to do loops :frown:
... so any help is greatly appreciated and of course +rep :biggrin:


P.S. I won't be telling what the league numbers are, but there are about 12 - 16 different leagues, knowing how many leagues there are is not relevant as the script I am required to write must work for any number of leagues.
Reply 1
I've never used matlab but surely there is a better way of representing that problem than a 5000 row, 2 column matrix. Why not have a row for each league and use a different column to represent each match?
Reply 2
Original post by Dragon
I've never used matlab but surely there is a better way of representing that problem than a 5000 row, 2 column matrix. Why not have a row for each league and use a different column to represent each match?


the question uses an 8 column, 5000+ row matrix, all ive done is simplified it by combining 2 of the columns (home goals and away goals) into the second column of this new matrix A, and the league column stays the same while eliminating all the rest of the information such as what teams played and on what date.
But thanks for the tip, thats one way i could try and solve it, by organising the leagues into columns instead, only if i knew how :/ & also it'd be hard to get the script to automatically create new columns for new leagues, as I'm not allowed to assume that there are a fixed number of leagues, the script must work for any number of leagues.
Reply 3
bump
Reply 4
last bump :c
Original post by Lewk
Hi, so basically I have a 2 column matrix which i've called 'A' and about 5,000+ rows. Column 1 is the league name and column 2 is the goals scored (home team and away team combined) for any given match, which is what each row represents - a match.

I need to write a script or function which will generate the league number that has the highest average number of goals scored per match. I'm still quite new to matlab so I have a lack of knowledge when it comes to functions, and I have little idea how to do loops :frown:
... so any help is greatly appreciated and of course +rep :biggrin:


P.S. I won't be telling what the league numbers are, but there are about 12 - 16 different leagues, knowing how many leagues there are is not relevant as the script I am required to write must work for any number of leagues.


First sort your matrix by league number using

B = sort(A,1)

That will get you all the data in order from each league, which you'll need for the next bit.

Now you want to work out the total number of goals scored in each league. do this with a while loop:


i = 2;
j = 1;
g = 0;
n = 0;
avGoals = 0;
while j<size(B,1)
while ((B(i,1) == B(i-1,1)) && (i<=size(B,1)))
g = g + B(i-1,2);
n = n + 1;
i= i+ 1;
end
if avGoals < (g./n)
leagueNum = B(i-1,1);
avGoals = g./n
end
n = 0;
j = i;
g = 0;
end

Once the code has run, leageNum should be the league that has the highest average number of goals scored per match... I haven't tested this code so I wouldn't be suprised if it doesn't work/doesn't even run! Also if two leagues have the same number of average goals per match, this code will only pick up on the first one (in numerical order)

Also I would suggest checking out the MATLAB website, it's of great help... You might want to check these:

http://www.mathworks.com/help/techdoc/ref/sort.html
http://www.mathworks.com/help/techdoc/math/f1-85462.html
http://www.mathworks.com/help/techdoc/ref/size.html
http://www.mathworks.com/help/techdoc/ref/while.html
(edited 13 years ago)

Quick Reply

Latest