Skip to content
Advertisement

How to create a tensor from a list of matrices in numpy?

JavaScript

How do I create a tensor T that is 3x2x3; as in, T[:,:,0] = A, T[:,:,1] = B, and T[:,:,2] = C? Also I may not know the number of matrices that I may be given before run time, so I cannot explicitly create an empty tensor before hand and fill it.

I tried,

JavaScript

but that gives me an array where T[0,:,:] = A, T[1,:,:] = B, and T[2,:,:] = C. Not what I wanted.

Advertisement

Answer

Is this what you’re after? I’ve used randint instead of rand to make it easier to see in the printed output that the arrays are lined up the way you wanted them.

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement