在Matlab中加快索引查找的好方法是什么?

时间:2016-05-30 09:55:19

标签: performance matlab

我有以下代码,执行了数千次。通过使用Profiler,我将第二行(new_inds=...)识别为整个代码中最耗时的行。是否有更好/更快的方法来执行此操作?

for j=1:length(cls)
    new_inds=[sti(:).time]==i & [sti(:).cluster]==cls(j);
    % more code here
    %
    %
    prob(j)=...
end
pr=prod(prob);

1 个答案:

答案 0 :(得分:2)

如果没有看到更多的代码很难明确告诉我,但我希望这会加快它的速度:

indexArg2 = [sti(:).cluster];
index1 = [sti(:).time]==i

for j=1:length(cls)
  new_inds=index1 & indexArg2==cls(j);
  % more code here
  %
  %
  prob(j)=...
end
pr=prod(prob);

我在此建议的是在j循环之外采用与j无关的索引部分。

P.S。有一种观点认为,您应该使用iijj作为计数器,而不是ij(因为它们代表复数)。我已阅读(但从未见过证明)使用iijjij更快...