Home > Software design >  How to make a 4D numpy array from 2 2D numpy array?
How to make a 4D numpy array from 2 2D numpy array?

Time:06-13

I have 2 2D numpy arrays with shape (160000,91).

160000 means all pixels of an image (200x800), 91 means # of images.

U(160000,91) and V(160000,91).

How to create a numpy array X with shape (91,200,800,2) with X(:,:,:,0) = U(91,200,800) and X(:,:,:,1) = V(91,200,800)?

CodePudding user response:

Try this

U = np.reshape(U, (91,200,800))
V = np.reshape(V, (91,200,800))

X = np.stack((U, V), axis=3)

CodePudding user response:

Transpose to move the batch dimension to axis 0. Then reshape the two arrays and stack them along a new axis 3:

import numpy as np

U = np.random.random((160000, 91))
V = np.random.random((160000, 91))
X = np.stack((np.reshape(U.T, (-1, 200, 800)),
              np.reshape(V.T, (-1, 200, 800))),
             axis=3)
print(X.shape)  # (91, 200, 800, 2)
  • Related