如何在numpy中构造向量的所有可能差异的矩阵

use*_*500 3 python numpy array-difference

我有一个一维数组,可以这样说:

import numpy as np
inp_vec = np.array([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)

现在,我想构造一个形式的矩阵

m = [[0, 1-2, 1-3], [2-1, 0, 2-3], [3-1, 3-2, 0]]
Run Code Online (Sandbox Code Playgroud)

当然,可以使用for循环来完成此操作,但是有没有更优雅的方法呢?

小智 5

我也找到了一种不错的方法:

np.subtract.outer([1,2,3], [1,2,3])
Run Code Online (Sandbox Code Playgroud)