假设我有一个大小为 n(非 1 的奇数)的矩阵,我想计算每个条目距中心的距离。例如,如果 n = 2,则矩阵为 5 x 5,要找到矩阵的中心,您需要执行以下操作:
import numpy as np
import math
center = math.floor(5/2)
Matrix[math.floor(5/2)][math.floor(5/2)] = 0
The center is zero because the distance to itself is 0. my approach is make the center like the origin of a coordinate plane and treat each of the 25 "squares" (5 by 5 matrix) as a dot in the center of each square and then calculate the euclidean distance that dot is from the center. visually:
到目前为止我的想法..
Matrix = [[0 for x in range(n)] for y in range(n)] #initialize the n by n matrix
for i in range(0, n):
for j in range(0, n):
Matrix[i][j] = ...
或者有更好的方法来找到距离矩阵吗?
输出应该是对称的,对于 n = 5 矩阵,它是
Matrix
[[2.82843, 2.23607, 2, 2.23607, 2.82843],
[2.23607, 1.41421, 1, 1.41421, 2.23607],
[2, 1, 0, 1, 2],
[2.23607, 1.41421, 1, 1.41421, 2.23607],
[2.82843, 2.23607, 2, 2.23607, 2.82843]]
TIA