简体   繁体   中英

way to create a 3d matrix of 2 vectors and 1 matrix

Hello i have a question regarding a problem I am facing in python. I was studying about tensors and I saw that each row/column of a tensor must have the same size. Is it possible to create a tensor of perhaps a 3d object or matrix where lets say we have 3 axis : x,y,z In the x axis I want to create a vector to work as an index. So let x be from 0 to N Then on the y axis I want to have N random integer vectors of size m (where mm Is it possible? My first approach was to create a big vector of Nm and a big matrix of (N m,N m) dimensions where i would store all my random vectors and matrices and then if I wanted to change for example the my second vector then i would have to play with the indexes. However is there another way to approach this problem with tensors or numpy that I m unaware of? Thank you in advance for your advices

First vector, N = 3, [1,2, 3] Second N vectors with length m, m = 2 [[4,5], [6,7], [7,8]]

So, N matrices of size (m,m) [[[1,1], [2,2]], [[1,1], [2,2]], [[1,1], [2,2]] ]

Lets create numpy arrays from them.

import numpy as np
N = 3
m = 2
a = np.array([1,2,3])
b = np.random.randn(N, m)
c = np.random.randn(N, m, m)

You see the problem here? The last matrix c has already 3 dimensions according to your definitions.

Your argument can be simplified.

Let's say our final matrix is - a = np.zeros((3,2,2)) # 3 dimensions, x,y,z 1) For first dimension -

a[0,:,:] = 0 # first axis, first index = 0

a[1,:,:] = 1 # first axis, 2nd index = 1

a[2,:,:] = 2 # first axis, 3rd index = 2

2) Now, we need to fill up the rest of the positions, but dimensions don't match up.

So, it's better to create separate tensors for them.

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