【发布时间】:2016-10-03 15:37:12
【问题描述】:
我正在尝试在 python 中重写以下 matlab 代码:
repmat(points, 1, length(points)) - repmat(points', length(points),1);
points 是一个包含多个点的弧度值的数组。
上面的代码给了我这样的矩阵输出:
0 1 2 0 1 2 0 1 2
-1 0 1 -1 0 1 -1 0 1
-2 -1 0 -2 -1 0 -2 -1 0
0 1 2 0 1 2 0 1 2
-1 0 1 -1 0 1 -1 0 1
-2 -1 0 -2 -1 0 -2 -1 0
0 1 2 0 1 2 0 1 2
-1 0 1 -1 0 1 -1 0 1
-2 -1 0 -2 -1 0 -2 -1 0
我可以很容易地操纵它来获得每个点到每个其他点的距离。
我只是想知道是否有 one liner 方法可以使用 numpy 来做到这一点?
我尝试了以下方法,但没有成功:
np.tile(points, (1, len(points))) -
np.tile(points.T, (len(points), 1))
有人有什么想法吗?
【问题讨论】:
-
仅供参考,在 MATLAB 中,您实际上更喜欢使用
bsxfun而不是repmat:bsxfun(@minus,points,points')。bsxfun是Suever提到的Python自动广播的MATLAB模拟。 -
@Dan 遗憾的是,没有公共链接。
标签: python matlab numpy matrix