1

I have numpy array like:

A = np.zeros((X,Y,Z)) 

Later I fill the array with values and I need to find x,y coordinates according to values in arrays on z axis to replace it with different array.

Example withZ=2 I have array

A = [ [ [1,2], [2,1], [0, 0] ],
      [ [1,1], [1,1], [0, 0] ], ]

I need something like

A[ where A[x,y,:] == [1,2] ] = [2,1]

What will produce array

A = [ [ [2,1], [2,1], [0, 0] ],
      [ [1,1], [1,1], [0, 0] ], ]

Is it possible to achieve that somehow simple? I would like to avoid iteration over the x,y coordinates.

2
  • 1
    Could you add a sample case and the expected output? Commented Oct 8, 2016 at 10:43
  • @Divakar Done. If it is not enough, then let me know. Commented Oct 8, 2016 at 10:49

1 Answer 1

3

One vectorized approach -

A[(A == [1,2]).all(-1)] = [2,1]

Sample run -

In [15]: A
Out[15]: 
array([[[1, 2],
        [2, 1],
        [0, 0]],

       [[1, 1],
        [1, 2],
        [0, 0]]])

In [16]: A[(A == [1,2]).all(-1)] = [2,1]

In [17]: A
Out[17]: 
array([[[2, 1],
        [2, 1],
        [0, 0]],

       [[1, 1],
        [2, 1],
        [0, 0]]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.