I have data stored in three different arrays: A, B and C.
I also have an integer array, Z, that has either 1, 2 or 3 in it.
A, B, C and Z have the same shape.
I want to create a new array, D, that contains the value listed in A if the corresponding element in Z is 1, the value listed in B if the corresponding element in Z is 2 or the value listed in C if the corresponding element in Z is 3.
To do this, I have written code with nested versions of numpy.where. The code however looks ugly. Is there a better way to do the same ?
import numpy as np
#Create arrays that hold the data
A = np.array([1.,1.,1.])
B = A * 2
C = A * 3
#Create an array that hold the zone numbers
Z = np.array([1,2,3])
#Now calculate the new array by figuring out the appropriate zone at each location
D = np.where(Z==1,A,np.where(Z==2,B, np.where(Z==3,C,0.0)))
#Output
print A
print B
print C
print D
[ 1. 1. 1.]
[ 2. 2. 2.]
[ 3. 3. 3.]
[ 1. 2. 3.]