计算matlab中先前出现的排名

时间:2018-03-13 15:28:12

标签: matlab

我正在努力为我的国际象棋俱乐部做一些排名。我认为这是一个很好的机会来编写代码来找到ELO。我编码它的方式是每年我们将排名重置为1200然后计算它。所以我有矢量年,赢家,输家,我想做elo。赢家和输家只是识别人的数字100-125,我有3年的数据。

所以我正在努力做到这一点。我的斗争是弄清楚如何找到最近的分数,因为当我查看每一行时,我需要搜索最后一次在赢或输列中调用某人的ID。另外,如果年份发生变化,我需要重置ELO分数(我认为应该使用唯一的(年份))。

这是我尝试过的(disp(' here')只是为了告诉我它是否运行,它从未这样做):

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

1 个答案:

答案 0 :(得分:1)

i=1的代码中断,这是最外层for循环的第一步

 for i = 1:length(Season2014toNow);

对于i = 1,我们在内部循环中得到i-1=0

for j = i-1:-1:1;

这是不可能的指令"从0开始减去1直到我们到达1"。

围绕这个的一个方法是为i=1添加一个例外,它允许我们跳过这个破坏程序的代码。例如

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

围绕这个的第二个路线是使用find功能。如果我对您的代码的理解是正确的,那么您的目标是找到当年第一次出现WID==WinTeamID

%% 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')

可以将其构建为代替代码中的j循环的东西

PS。我无法看到您的CalcElo代码。我认为它有效并且是正确的。

PPS。进行最佳实践MATLAB编程的其他小修正方法是缩进代码的易读性(在MATLAB中突出显示并按 Ctrl + i / Cmd + i ),用%%添加注释来解释代码的作用,并将j循环放入函数并调用该函数,而不是将相同的代码复制出来两次。