2021年3月5日星期五

How to create a 4 dimensional numpy array from a 3 dimensional numpy array filling with zeros?

The problem

Create a higher dimensional NumPy array with zeros on the new dimensions

Details

Analyzing the last dimension, the result is similar to this:

(not an actual code, just a didactic example)

a.shape = (100,2,10)   a[0,0,0]=1  a[0,0,1]=2    ...  a[0,0,9]=10    b.shape = (100,2,10,10)  b[0,0,0,:]=[0,0,0,0,0,0,0,0,0,1]  b[0,0,1,:]=[0,0,0,0,0,0,0,0,2,1]   b[0,0,2,:]=[0,0,0,0,0,0,0,3,2,1]  ...  b[0,0,2,:]=[10,9,8,7,6,5,4,3,2,1]         a -> b   

The objective is to transform from a into b. The problem is that is not only filled with zeros but has a sequential composition with the original array.

Simpler problem for better understanding

Another way to visualize is using lower-dimensional arrays:

We have this:

a = [1,2]  

And I want this:

b = [[0,1],[2,1]]  

Using NumPy array and avoiding long for loops.

2d to 3d case

We have this:

a = [[1,2,3],[4,5,6],[7,8,9]]  

And I want this:

b[0] = [[0,0,1],[0,2,1],[3,2,1]]  b[1] = [[0,0,4],[0,5,4],[6,5,4]]  b[2] = [[0,0,7],[0,8,7],[9,8,7]]  

I feel that for the 4-dimensional problem only one for loop with 10 iterations is enough.

https://stackoverflow.com/questions/66492847/how-to-create-a-4-dimensional-numpy-array-from-a-3-dimensional-numpy-array-filli March 05, 2021 at 08:45PM

没有评论:

发表评论