矢量化加法
Vectorizing addition
假设我有两个 numpy 数组 a[n,3]
和 b[m,3]
如何计算 c[n,m,3]
,而不求助于 for
循环,例如:
c[i,j,:] = a[i,:] + b[j,:]
试试这个:
c = a[:, None, :] + b[None, :, :]
假设我有两个 numpy 数组 a[n,3]
和 b[m,3]
如何计算 c[n,m,3]
,而不求助于 for
循环,例如:
c[i,j,:] = a[i,:] + b[j,:]
试试这个:
c = a[:, None, :] + b[None, :, :]