我有一个由值填充的numpy 2D数组(50x50),我想将这个2D数组压缩成一列(2500x1),但是值的位置很重要。可以将索引转换为空间坐标,因此我需要另外两个(x,y)(2500x1)数组,以便检索相应值的x,y空间坐标。
例如:
例如:
My 2D array:
--------x-------
[[0.5 0.1 0. 0.] |
[0. 0. 0.2 0.8] y
[0. 0. 0. 0. ]] |
My desired output:
#Values
[[0.5]
[0.1]
[0. ]
[0. ]
[0. ]
[0. ]
[0. ]
[0.2]
...],
#Corresponding x index, where I will retrieve the x spatial coordinate from
[[0]
[1]
[2]
[3]
[4]
[0]
[1]
[2]
...],
#Corresponding y index, where I will retrieve the x spatial coordinate from
[[0]
[0]
[0]
[0]
[1]
[1]
[1]
[1]
...],
有什么提示可以做到这一点吗?我试过几种方法,但它们都没有起作用。
np.unravel_index(range(a.size), a.shape)
更简单。 - bb1