我一直试图得到矩阵(A)中包含元素nd的所有行索引。你知道吗
A的大小是4米×4,这个操作大约需要12秒。你知道吗
链接到文件:data
# Download the file named elementconnectivity before running this small script
A=np.loadtxt('elementconnectivity')
A=A.astype(int)
nd=1103
v=[i for i in range(len(A)) if nd in A[i]]
有没有更快的方法来实现这一点?你知道吗
Tags:
我认为更好的方法是使用迭代器而不是列表
因为您使用的是
numpy
,所以使用更多的numpy函数可以大大加快速度。您当前在我的系统上的方法:相反,这要快40倍:
您还可以查看
np.in1d
,它类似于我上面使用的A == nd
,但是可以与列表(类似于A==nd1或nd2或nd3)进行比较。你知道吗相关问题 更多 >
编程相关推荐