2

我有一个大np.array

[1.,    2.,    3.,    ...]
[1000., 1001., 1002., ...]
[2000., 2001., 3002., ...]
[3000., 3001., 3002., ...]

例如。我想将每列中的每个元素除以该列的模块。这是第一列的示例:

col_1 = {1, 1000, 2000, 3000}

module_col_1 =

new_col_1 = {1/模块、1000/模块、2000/模块、3000/模块}

使用for循环太慢了。我怎么能在没有for循环的情况下进行这个计算?

4

2 回答 2

2

像这样?

M = np.array([[1,2,3],[1000,1001,2002],[2000,2001,2002]])
M / np.sqrt(np.sum(M**2,axis=0))
array([[4.47213551e-04, 8.93890482e-04, 1.05959998e-03],
       [4.47213551e-01, 4.47392186e-01, 7.07106384e-01],
       [8.94427102e-01, 8.94337427e-01, 7.07106384e-01]])
于 2019-11-07T14:08:43.450 回答
1
modules = np.sqrt(np.sum(arr * arr, axis=0))
new_arr = arr / modules
于 2019-11-07T14:08:33.263 回答