对于给定的 2D 矩阵,np.array([[1,3,1],[2,0,5]])
如果需要计算矩阵中每行的最大值(不包括其自己的列),并使用预期的示例 return np.array([[3,1,3],[5,5,2]])
,那么最有效的方法是什么?目前我用一个循环来实现它来排除它自己的 col 索引:
n=x.shape[0]
row_max_mat=np.zeros((n,n))
rng=np.arange(n)
for i in rng:
row_max_mat[:,i] = np.amax(s_a_array_sum[:,rng!=i],axis=1)
Run Code Online (Sandbox Code Playgroud)
有没有更快的方法呢?
与您的想法类似(一一排除列),但带有索引:
mask = ~np.eye(cols, dtype=bool)
a[:,np.where(mask)[1]].reshape((a.shape[0], a.shape[1]-1, -1)).max(1)
Run Code Online (Sandbox Code Playgroud)
输出:
array([[3, 1, 3],
[5, 5, 2]])
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
255 次 |
最近记录: |