【问题标题】:Calculate Ranking from previous occurrence in matlab计算matlab中先前出现的排名
【发布时间】:2018-03-13 15:28:11
【问题描述】:

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

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

这是我尝试过的(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

【问题讨论】:

  • 测试“WinteamID”向量是否为数字,例如。 'WinteamID(1)*WinteamID(2)' 是否返回值?如果“WinteamID”是文本,则必须使用“strcmp()”来获取“if”语句的逻辑值
  • 如果我这样做,它确实会返回一个数字
  • 我使用的所有东西都是双精度的向量/矩阵

标签: matlab


【解决方案1】:

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

 for i = 1:length(Season2014toNow);

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

for j = i-1:-1: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),添加带有%% 的 cmets 来解释代码在做什么,并将你的 j 循环放入一个函数并调用该函数,而不是将相同的代码复制两次。

【讨论】:

  • 我仍然没有显示“这里”。我是否正确使用了休息时间?因为我想在找到团队 ID 上次发生的时间后退出循环
  • A break 将退出 for 或 while 循环...所以是的,如果我正确理解您的代码,他们可能会做您想做的事。无论如何,disp() 出现在break 之前,因此即使休息时间有些奇怪,您也应该看到显示“此处”。尝试逐行执行代码?
  • 一些细节在这里:uk.mathworks.com/help/matlab/matlab_prog/… 然后使用工具栏中的'step'按钮逐行浏览代码
  • 所以我这样做了,但我还没有找到 j 实际上倒退的情况。 winteamId 的长度为 214000,有 315 个唯一数字(我只是在我的帖子中做了一个从 100 到 125 的子集)。
  • 那么我打破了 if/elseif/else 的所有条件的问题吗?这样我只评估第一个 j?编辑:是的
猜你喜欢
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多