本文介绍了是否有类似"ismember"的功能?但是效率更高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,A是数字set.b是元素.
For example,A is the number set.b is elements.
我想测试b中的数字是否是集合A的元素.
I want to test whether the number in b is the element of the set A.
我知道matlab函数"ismember"可以做到这一点,但是当我使用它一百万次时,它的速度还不够快.
I know the matlab function "ismember" could do this ,but it's not fast enough when I use it one million times.
b=[1,2,9,100];
A=[1,2,3,4,5,6,7,8,9];
tic;for ii=1:1e6,ismember(b,A);end;toc
Elapsed time is 45.714583 seconds.
我想返回[1,1,1,0],因为1,2,9位于集合A中,而100没有.
I want to return [1,1,1,0],because 1,2,9 are in the set A,while 100 is not.
您知道某些功能,例如ismember还是比"ismember"更有效的方法?
Do you know some functions like ismember or some ways more efficient than "ismember"?
推荐答案
您可以使用混合版本,即ismemberoneoutput
.混合版本要快得多.
You can use the mex version, i.e. ismemberoneoutput
. The mex version is much faster.
b=[1,2,9,100];
A=[1,2,3,4,5,6,7,8,9];
tic;for ii=1:1e5,ismember(b,A);end;toc
%Elapsed time is 9.537219 seconds. On my pc
% A must be sorted!!! In this example it is already sorted,
% so no need for this here.
tic;for ii=1:1e5,builtin('_ismemberoneoutput',b,A);end;toc
%Elapsed time is 0.376556 seconds. On my pc
这篇关于是否有类似"ismember"的功能?但是效率更高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!