小编zd5*_*151的帖子

Python相当于MATLAB的"ismember"函数

在尝试优化代码的许多尝试之后,似乎最后一个资源是尝试使用多个核运行下面的代码.我不知道如何转换/重新构建我的代码,以便使用多个内核可以更快地运行.如果我能获得指导以实现最终目标,我将不胜感激.最终目标是能够尽快为阵列A和B运行此代码,其中每个阵列包含大约700,000个元素.这是使用小数组的代码.700k元素数组被注释掉了.

import numpy as np

def ismember(a,b):
    for i in a:
        index = np.where(b==i)[0]
        if index.size == 0:
            yield 0
        else:
            yield index


def f(A, gen_obj):
    my_array = np.arange(len(A))
    for i in my_array:
        my_array[i] = gen_obj.next()
    return my_array


#A = np.arange(700000)
#B = np.arange(700000)
A = np.array([3,4,4,3,6])
B = np.array([2,5,2,6,3])

gen_obj = ismember(A,B)

f(A, gen_obj)

print 'done'
# if we print f(A, gen_obj) the output will be: [4 0 0 4 3]
# notice that the output array needs to …
Run Code Online (Sandbox Code Playgroud)

python optimization matlab numpy

20
推荐指数
2
解决办法
2万
查看次数

标签 统计

matlab ×1

numpy ×1

optimization ×1

python ×1