I would like to expand on the problem that I have already talked about in https://stackoverflow.com/a/2129609/169 . It dealt with numpy 2D arrays, and I would like to do the same with a 3-dimensional array.
I would like to "move" the elements of a 2D array to the new coordinates, which are stored in two other arrays. I want to automate this because my arrays are actually large (400x200x100). Some values will not find its coordinates and will not be used. Some of these coordinates are masked, which I indicated in the example below using the value 0. If the coordinate is masked, the elements in the array that I want to rearrange will not.
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.]], [[ 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.]], [[ 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.]]]) 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]], [[-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]]])
So, the expected array looks like this:
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]], [[-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]]])
I am trying the piece of code that I received with the last message.
b = np.zeros_like(IRtest) for i in range(IRtest.shape[1]): for j in range(IRtest.shape[2]): for k in range(IRtest.shape[0]): b[k, j, i] = IRtest[k,my[k,j,i],mx[k,j,i]]*(mx[k,j,i]!=-1)*(my[k,j,i]!=-1) b
But the result is not what I expected:
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]], [[-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]]])