我如何找到nxn中心的索引位置

时间:2018-12-01 05:44:07

标签: math matrix

我想获取矩阵中心位置的索引。有什么表达式可以做到这一点?

enter image description here

1 个答案:

答案 0 :(得分:0)

由于n是奇数,因此可以通过mat[n/2][n/2]找到指定的位置。现在,通过f(n) = n * (n - 1) / 2 + (n + 1) / 2 = n^2 / 2 - n/2 + n/2 + 1/2 = (n^2 + 1)/2查找位置的行索引。

例如f(3) = 3 * 1 + 2 = 5f(5) = 5 * 2 + 3 = 13f(7) = 7 * 3 + 4 = 25