'How to make a vector of matrices in numpy

I would like to create a vector of the same matrix in numpy (so as an array). Let's say the matrix is:

w = np.array([[1,2],
              [3,4],
              [5,6]])

Then, how can I create a vector of a fixed length with the matrix w in every position?

That is: vector[0] = ... = vector[n] = w



Solution 1:[1]

Not sure of the exact expected output, to to create an additional dimension you could use numpy.tile:

n = 3
vector = np.tile(w, (n, 1, 1))

NB. vector is not really a (1D) vector it is a 3D array

output:

# vector
array([[[1, 2],
        [3, 4],
        [5, 6]],

       [[1, 2],
        [3, 4],
        [5, 6]],

       [[1, 2],
        [3, 4],
        [5, 6]]])

# vector[0]
array([[1, 2],
       [3, 4],
       [5, 6]])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway