问题描述
继续我的研究,
我需要另一个与ismember
类似的逻辑,其执行时间更快.这部分代码和矩阵.
I need another similar logic with ismember
that has execution time faster. this part of my code and the matrix.
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检查了该代码.我在这部分中使我的代码花了很长时间执行时间.
I checked that code by using Profile viewer. I got that in this part that made my code took time execution slowly.
任何人都有关于此逻辑的经验,请分享.谢谢
Anyone has experience about this logic, please share. thanks
推荐答案
MATLAB具有多个未公开的内置函数,可帮助您实现与其他功能相同的结果,只是速度更快.
MATLAB has several undocumented built-in functions which can help you achieve the same results as other functions, only faster.
根据您的情况,您可以使用 ismembc
:
In your case, you can use ismembc
:
done = all(ismembc(StartPost(h, 1:4), sort(StartPost(h-1, 1:4)))));
请注意,ismembc(A, B)
要求对矩阵B
进行排序,并且不包含任何NaNs值.
Note that ismembc(A, B)
requires matrix B
to be sorted and not to contain any NaNs values.
这是您的示例的执行时间差:
Here's the execution time difference for your example:
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倍.
这篇关于还有一个比ismember更快的类似逻辑是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!