简体   繁体   English

将一维数组重塑为蒙版二维数组

[英]Reshape a 1D array to a masked 2D array

OK so I have this 2D array: 好,所以我有这个2D数组:

print indgrid

[[0 6 9 3 1]
[5 9 7 4 5]
[2 8 3 8 4]
[5 6 2 6 8]
[5 3 7 7 0]]

I then apply a mask to it: 然后,我给它加上一个面具:

print mask

[[False False  True False False]
 [False  True  True  True False]
 [ True  True  True  True  True]
 [False  True  True  True False]
 [False False  True False False]]


print indgrid[mask]

[9 9 7 4 2 8 3 8 4 6 2 6 7]

So this is now a 1D array of values that have been taken out of the indgrid array. 因此,这是从indgrid数组中取出的一维值数组。 I then want to perform some calculations on these values and then when I am done, put the values calculated from each element into a 2D array which is the same shape as the indgrid array, so I would end up with this: 然后,我想对这些值执行一些计算,然后在完成后,将从每个元素计算出的值放入与indgrid数组具有相同形状的indgrid数组中,因此我将indgrid

[[ 0 0 f 0 0 ]
[ 0 f f f 0 ]
[ f f f f f ]
[ 0 f f f 0]
[ 0 0 f 0 0 ]]

where f just represents a float that I have calculated. 其中f代表我计算出的浮点数。 What is the best way of doing this? 最好的方法是什么? Thanks. 谢谢。

Forgive the terrible variable names: 原谅可怕的变量名:

f_values = some_function_of(indgrid[mask])
result = np.zeros(indgrid.shape)
result[mask] = f_values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM