Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这段 Python 代码,它在 for 循环中填充了一个 2d 矩阵
img=zeros((len(bins_x),len(bins_y))) for i in arange(0,len(ix)): img[ix[i]][iy[i]]=dummy[i]
是否可以对最后两行代码使用矢量运算?还有什么东西可以加快计算速度吗?
如果 ix, iy 是索引序列:
img[ix, iy] = dummy
使用numpy可能很有用。特别是,重塑方法可能很有用。这是一个示例(改编自第二个链接):
>>> import numpy as np >>> a = np.array([1,2,3,4,5,6]) >>> np.reshape(a, (3,2)) array([[1, 2], [3, 4], [5, 6]])