是否有类似“ismember"的功能?但更有效率? [英] Is there a function like "ismember" but more efficient?

查看:23
本文介绍了是否有类似“ismember"的功能?但更有效率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,A是集合,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"?

推荐答案

可以使用mex版本,即ismemberoneoutput.mex 版本要快得多.

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"的功能?但更有效率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆