如何将1D阵列转换为对称的3D阵列?

时间:2020-11-27 21:19:39

I have a symmetrical 1D numpy array, for example, something like this:

我有一个对称的1D numpy数组,例如,像这样:

0 1 2 1 0

How could I turn this into a 3D array (kinda similar to a gaussian kernel), with the value 2 at the center?

我怎么能把它变成一个3D数组(有点类似于高斯内核),中心的值为2?

As an example of what I mean (though the math is likely not right), in 2D this would be something like this (though I need it to be 3D):

作为我的意思的一个例子(尽管数学可能不对),在2D中这将是这样的(虽然我需要它是3D):

0   0   0   0   0
0  0.5  1  0.5  0
0   1   2   1   0
0  0.5  1  0.5  0
0   0   0   0   0

1 个解决方案

#1


1  

Acknowledging that this is not a Gaussian kernel, here's how you calculate it:

承认这不是高斯内核,这是你如何计算它:

center = a[a.size // 2]
(a[:,    np.newaxis].repeat(a.size, axis=1) * a)\
  [:, :, np.newaxis].repeat(a.size, axis=2) * a \
/ center ** 2

(Not gonna paste the whole output here.)

(不要在这里粘贴整个输出。)

#1


1  

Acknowledging that this is not a Gaussian kernel, here's how you calculate it:

承认这不是高斯内核,这是你如何计算它:

center = a[a.size // 2]
(a[:,    np.newaxis].repeat(a.size, axis=1) * a)\
  [:, :, np.newaxis].repeat(a.size, axis=2) * a \
/ center ** 2

(Not gonna paste the whole output here.)

(不要在这里粘贴整个输出。)