小编ise*_*gar的帖子

在 numpy 中从 1d 数组创建 nxn 方阵的最快方法

假设有以下 numpy 数组:

arr = np.array([0, 1, 2, 3, 4]) # can be any array
Run Code Online (Sandbox Code Playgroud)

我想知道生成以下操作的最快方法:

n = arr.shape[0]
result = np.tile(arr, (n, 1)) - arr.reshape((-1, 1))
print(result):

array([[ 0,  1,  2,  3,  4],
       [-1,  0,  1,  2,  3],
       [-2, -1,  0,  1,  2],
       [-3, -2, -1,  0,  1],
       [-4, -3, -2, -1,  0]])
Run Code Online (Sandbox Code Playgroud)

(1) 如何有效地创建矩阵“结果”(因为 n >> 0 可能非常大)?

(2) 这个矩阵有特定的名称吗?

python arrays numpy scipy

3
推荐指数
1
解决办法
631
查看次数

标签 统计

arrays ×1

numpy ×1

python ×1

scipy ×1