【问题标题】:What is another similar logic that is faster than ismember?还有什么比ismember更快的类似逻辑?
【发布时间】:2012-08-31 16:32:56
【问题描述】:

继续我的研究,

我需要另一个与ismember 类似的逻辑,它的执行时间更快。 我的这部分代码和矩阵。

StartPost =

    14    50    30     1    72    44    76    68    63    80    25    41;
    14    50    30     1    61    72    42    46    67    77    81    27;
    35    23     8    54    19    70    48    75    66    79     2    84;
    35    23     8    54    82    72    78    68    19     2    48    66;
    69    24    36    52    63    47     5    18    11    82     1    15;
    69    24    36    52    48    18     1    12    80    63     6    84;
    73    38    50     7     1    33    24    68    29    20    62    84;
    73    38    50     7    26    61    65    32    22    18     2    69]

for h=2:2:8,
    ...
       done=all(ismember(StartPost(h,1:4),StartPost(h-1,1:4)));
    ...
end

我使用 Profile viewer 检查了该代码。我在这部分得到了这一点,这使得我的代码执行缓慢。

谁有这个逻辑的经验,请分享。谢谢

【问题讨论】:

  • 您在寻找isequal吗? mathworks.com/help/techdoc/ref/isequal.html> 我不确定它是否更快(尽管它可能是),但它肯定更具可读性。
  • @beaker isequal 不会在这里删减它,因为 ismember 也会为具有不同顺序的相同元素的两行返回 true,而 isequal 不会。
  • @EitanT - 很好,谢谢。
  • 我不知道它是否更快,但原则上isempty(intersect(StartPost(h,1:4),StartPost(h-1,1:4)))应该产生相同的结果。

标签: matlab random


【解决方案1】:

MATLAB 有几个undocumented built-in functions 可以帮助您实现与其他函数相同的结果,只是速度更快。

在你的情况下,你可以使用ismembc:

done = all(ismembc(StartPost(h, 1:4), sort(StartPost(h-1, 1:4)))));

请注意,ismembc(A, B) 需要矩阵 B 进行排序并且不包含任何 NaN 值。

这是您的示例的执行时间差异:

tic
for h = 2:2:8
   done = all(ismember(StartPost(h, 1:4), StartPost(h-1, 1:4)));
end
toc

Elapsed time is 0.029888 seconds.


tic
for h = 2:2:8
   done = all(ismembc(StartPost(h, 1:4), sort(StartPost(h-1, 1:4))));
end
toc

Elapsed time is 0.006820 seconds.

这大约快 50 倍。

【讨论】:

  • 先生...在我的旧版本 matlab (R2009a) 中没有 ismembc。我试图通过帮助找到它,我没有找到它。
  • 抱歉,我可以使用 ismembc。
  • 好的,谢谢先生,您的信息。但是为什么我在完整代码中使用它比ismember需要更长的时间。
  • 不应该。你能提供基准测试结果/时间测量吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 2015-07-20
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多