通过掩码移动知道新坐标的2D数组的值

时间:2014-11-16 12:55:27

标签: python arrays numpy coordinates

我想将2D数组的元素“移动”到新坐标,这些坐标存储在其他2个数组中。我希望自动化这个,因为实际上我的阵列很大(400x200x100)。 有些值不会找到他的坐标而不会被使用, 其中一些坐标被屏蔽,我在下面的例子中使用值0表示。如果坐标被屏蔽,我想要重新洗牌的数组中的元素将不会被使用。

import numpy as np

#My new coordinates in X and Y directions   
mx = np.array([[ 1.,  2.,  3.,  4.,  0.],
       [ 1.,  2.,  3.,  4.,  0.],
       [ 1.,  2.,  3.,  4.,  0.],
       [ 1.,  2.,  3.,  4.,  0.],
       [ 1.,  2.,  3.,  4.,  0.]])

my = np.array([[ 0.,  2.,  2.,  2.,  2.],
       [ 0.,  3.,  3.,  3.,  3.],
       [ 0.,  4.,  4.,  4.,  4.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

#The array with values to move
IRtest = np.array([[-0.07383495, -0.08606554, -0.08480594, -0.08099556, -0.08218414],
       [-0.07866761, -0.08373   , -0.08253587, -0.08106102, -0.08220205],
       [-0.07727436, -0.08271511, -0.0807254 , -0.07832416, -0.08021686],
       [-0.07612349, -0.08190446, -0.07996929, -0.07842754, -0.08024891],
       [-0.07488144, -0.08150557, -0.08038229, -0.07895656, -0.07997815]])

#Creation of zeros array to get new array
b = np.zeros((5,5))    

# I tried this but it doesn't work...
for i in range(IRtest.shape[0]):
    for j in range(IRtest.shape[1]):
        b[my[i,j], mx[i,j]] = IRtest[i,j]

plt.imshow(b)
plt.colorbar()
plt.show()

所以数组预期如下:

array_expected = np.array([[-0.08271511, -0.0807254 , -0.07832416, -0.08021686, 0],
       [-0.08190446, -0.07996929, -0.07842754, -0.08024891, 0],
       [-0.08150557, -0.08038229, -0.07895656, -0.07997815, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])

-----编辑以后-----------------

在这个“方向”中更好:

for i in range(IRtest.shape[0]):
    for j in range(IRtest.shape[1]):
        b[j, i] = IRtest[my[j,i],mx[j,i]]

我明白了:

array([[-0.08606554, -0.0807254 , -0.07832416, -0.08021686, -0.07727436],
       [-0.08606554, -0.07996929, -0.07842754, -0.08024891, -0.07612349],
       [-0.08606554, -0.08038229, -0.07895656, -0.07997815, -0.07488144],
       [-0.08606554, -0.08480594, -0.08099556, -0.08218414, -0.07383495],
       [-0.08606554, -0.08480594, -0.08099556, -0.08218414, -0.07383495]])

所以处理蒙面值的最后一个问题......

所以我试试:

mask_mx = np.array([[False, False, False, False, True],
        [False, False, False, False, True],
        [False, False, False, False, True],
        [False, False, False, False, True],
        [False, False, False, False, True]], dtype=int)

mask_my = np.array([[True, False, False, False, False],
        [True, False, False, False, False],
        [True, False, False, False, False],
        [True, True, True, True, True],
        [True, True, True, True, True]], dtype=int)


mx3 = np.where(mask_mx, 'nan', mx)

my3 = np.where(mask_my, 'nan', my)

for i in range(IRtest.shape[0]):
    for j in range(IRtest.shape[1]):
        b[j, i] = IRtest[my3[j,i],mx3[j,i]]

但我得到下面的错误,它不像'nan'那样坐标: int()的基数为10的无效文字:'nan'

3 个答案:

答案 0 :(得分:1)

您的代码确实有效,但您的坐标数组并不是您想要的。让我们分析你的双重循环:

i为2且j为1的迭代期间,您正在有效地处理此案例:

b[my_test[2,1], mx_test[2,1]] = IRtest[2,1]

IRtest[2,1]-0.08271511my[2,1]4mx[2,1]2,因此以上一行归结为:

b[4, 2] = -0.08271511

这正是您在输出中看到的内容。

基本上问题是您应该更改索引数组mymx以获得所需的输出。

答案 1 :(得分:1)

您可能希望使用-1作为掩码而不是0,因此您可以在IRtest中访问0索引。

mx = np.array([[ 1.,  2.,  3.,  4.,  -1.],
               [ 1.,  2.,  3.,  4.,  -1.],
               [ 1.,  2.,  3.,  4.,  -1.],
               [ 1.,  2.,  3.,  4.,  -1.],
               [ 1.,  2.,  3.,  4.,  -1.]])

my = np.array([[ 2.,  2.,  2.,  2.,  2.],
               [ 3.,  3.,  3.,  3.,  3.],
               [ 4.,  4.,  4.,  4.,  4.],
               [ -1.,  -1.,  -1.,  -1.,  -1.],
               [ -1.,  -1.,  -1.,  -1.,  -1.]])

b = np.zeros_like(IRtest)

for i in range(IRtest.shape[0]):
    for j in range(IRtest.shape[1]):
        b[j, i] = IRtest[my[j,i],mx[j,i]]*(mx[j,i]!=-1)*(my[j,i]!=-1)

答案 2 :(得分:0)

感谢numpy的花式索引,循环不是必需的,事实上,如果没有它,你的代码运行速度会快得多。

另外,请注意:

  1. 你的索引数组最好用int-arrays表示(fancy-indexing不能用于浮点数)
  2. 掩码最好表示为bool-arrays(允许逻辑操作为按位操作,内存效率更高,速度更快)。
  3. 因此需要进行一些预处理:

    mx = np.array(mx, dtype=int)
    my = np.array(my, dtype=int)
    mask_mx = np.array(mask_mx, dtype=bool)
    mask_my = np.array(mask_my, dtype=bool)
    unified_mask = mask_my | mask_mx  # logical-or as bitwise-or
    

    所以,首先没有掩码,你可以简单地对你的数组进行索引:

    b = IRtest[my,mx]
    

    然后要应用蒙版,您可以这样做:

    b *= ~unified_mask
    

    或显式分配零(再次,花式索引,这次使用掩码):

    b[unified_mask] = 0
    

    作为一个单行:

    b = IRtest[my,mx] * ~unified_mask
    

    这比循环更短,速度更快。整洁,是吗?

    通过numpy,通常可以避免循环。

相关问题