简体   繁体   中英

numpy array of numpy arrays of numpy arrays

I have a numpy object array of size (2x3). Lets call it M1 . In M1 there are 6 numpy arrays. The shapes of arrays in a given row of M1 are the same but differ from the shapes of arrays in any other row of M1 .

that is,

M1 = [ [A1 B1 C1]
       [D1 E1 F1] ]

A1,B1,C1,D1,E1,F1 are 2D numpy arrays. Shapes of A1, B1 and C1 are same. Shapes of D1,E1,F1 are same. Shape of A1 != D1 and so on.

Similarly I have

M2 = [ [A2 B2 C2]
       [D2 E2 F2] ]

Now I want a numpy array M3 which is of the same shape as M1.

M3 = [ [A3 B3 C3]
       [D3 E3 F3] ]

Where A3[0,0] = [A1[0,0] A2[0,0]] , A3[0,1] = [A1[0,1] A2[0,1]] and so on. (All the elements of M3 will be like this)

Is there a pythonic way to do this without using the for loops?

Also, I'd like to know what changes to make if I want A3[0,0] as:

A3[0,0] = [ [A1[0,0] A2[0,0]],
            [B1[0,0] B2[0,0]] ]

You can't get everything you want. You want to use the optimizations of numpy arrays (that is, you want to avoid for-loops), but you want the flexibility to have each row of M1 and M2 to have different shapes. But efficiency requires sacrificing flexibility in this case.

Just break M1 and M2 into different variables, one for each row. Call these M1a and M2a, M1b and M2b, .... Now you can create true 3d numpy arrays.

# building blocks ... like your A1, B1, etc
I2 = np.eye(2, dtype=np.int)

# First row of M1
M1a = np.array([I2, 2*I2, 3*I2])

# First row of M2
M2a = -M1a.copy()

# Stick them together such that M3a[0,0] = [M1a[0,0], M2a[0,0]]
M3a = np.transpose([M1a, M2a])

Now do the same for rows M1b, M2b, M3b using a building block of a different shape I3 = np.eye(3) . So you have a for-loop only over the last dimension.

I know, you want to vectorize the last dimension. But that's not possible if you want to maintain the flexibility of having each row use a different shape. Sorry! No free lunch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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