Reputation: 485
I am working with trying to do some rankings for my chess club. I figured this would be a good chance to work on coding to find ELO. The way I encode it is each year we reset the ranking to 1200 then calculate it. So I have the vectors year, winner, loser and I want to make elo. Winner and losers are just numbers 100-125 to identify person and I have 3 years of data.
So I am struggling with doing this correctly. My struggle is figuring out how to find the most recent score since when I look through each row, I need to search for the last time someone’s ID was called in either win or lose column. Additionally, if year changes, I need to reset the ELO score (I think using unique(year) should do).
Here is what I have tried (the disp('here') is just to show me if it runs, which it never does):
for i = 1:length(Season2014toNow);
WID = WinteamID(i);
LID = LoseteamID(i);
Seasoni = Season2014toNow(i);
for j = i-1:-1:1;
if WID == WinteamID(j) & Seasoni == Season2014toNow(j);
Win_old_team_elo(i) = Win_new_team_elo(j);
disp('here')
break
elseif WID == LoseteamID(j) & Seasoni == Season2014toNow(j);
Win_old_team_elo(i) = Loss_new_team_elo(j);
disp('here')
break
else
Win_old_team_elo(i) = 1200;
break
end
end
for j = i-1:-1:1;
if LID == WinteamID(j)& Seasoni == Season2014toNow(j);
Loss_old_team_elo(i) = Win_new_team_elo(j);
break
elseif LID == LoseteamID(j& Seasoni == Season2014toNow(j));
Loss_old_team_elo(i) = Loss_new_team_elo(j);
break
else
Loss_old_team_elo(i) = 1200;
break
end
end
[Win_new_team_elo(i),Loss_new_team_elo(i)] = CalcElo(Win_old_team_elo(i),Loss_old_team_elo(i)) ;
end
Upvotes: 0
Views: 42
Reputation: 1324
The code breaks for i=1
, which is the first step in the outermost for loop
for i = 1:length(Season2014toNow);
for i=1, we get a i-1=0
in the inner loops
for j = i-1:-1:1;
which is the impossible instruction "start at zero and subtract one until we get to 1".
One route around this is to add an exception for i=1
that allows us to skip this code that breaks the programme. eg.
for i = 1:length(Season2014toNow);
WID = WinteamID(i);
LID = LoseteamID(i);
Seasoni = Season2014toNow(i);
%% New if statement
if i==1 %%initialise scores and skip the code breaking i-1 loop
Win_new_team_elo(i) = 1200;
Loss_new_team_elo(i)= 1200;
else %% do the preexisting code
for j = i-1:-1:1;
if WID == WinteamID(j) & Seasoni == Season2014toNow(j);
Win_old_team_elo(i) = Win_new_team_elo(j);
disp('here')
break
elseif WID == LoseteamID(j) & Seasoni == Season2014toNow(j);
Win_old_team_elo(i) = Loss_new_team_elo(j);
disp('here')
break
else
Win_old_team_elo(i) = 1200;
break
end
end
for j = i-1:-1:1;
if LID == WinteamID(j)& Seasoni == Season2014toNow(j);
Loss_old_team_elo(i) = Win_new_team_elo(j);
break
elseif LID == LoseteamID(j& Seasoni == Season2014toNow(j));
Loss_old_team_elo(i) = Loss_new_team_elo(j);
break
else
Loss_old_team_elo(i) = 1200;
break
end
end
[Win_new_team_elo(i),Loss_new_team_elo(i)] = CalcElo(Win_old_team_elo(i),Loss_old_team_elo(i)) ;
end %% closing the if statement I added
end
A second route around this would be to use the find
function. If my understanding of your code is correct you are aiming to find the first occurrence of WID==WinTeamID
in the current year
%% Mask for current year
AllWinTeamID_ThisSeason = WinteamID(Season2014toNow==Seasoni)
%% Find the first ID that matches
% SYNTAX (condition, #items to return, order)
find(AllWinTeamID_ThisSeason== WID, 1,'first')
Which can be built into something to replace the j-loops in your code
PS. I can't see your CalcElo
code. I'm assuming it works and is correct.
PPS. Other minor fixes to get to best practice MATLAB programming would be to indent code for legibility (highlight in MATLAB and press Ctrl+i / Cmd+i), add comments with %%
to explain what bits of code are doing, and to put your j-loops into a function and call that function, rather than have the same code copied out twice.
Upvotes: 1