Create empty matrix Python

arrays, initialization, matrix, python, vector

Solution

I would recommend you use Numpy for this kind of stuff. It makes accessing columns or rows much easier. For your use case you'd do

import numpy as np

matrix = np.zeros((2,3,10))
second_col = matrix[:,1,:]

Numpy will also take better care of your data, and it implements a lot of the matrix algebra in Fortran or C so it's going to be a lot faster in the (possible) future when you're doing matrix multiplication and the likes.

Problem

I simply want to create an empty 10*3*2 array with Python. I first thought of these one, but this is not working: ``` parameters = [ [ [] * 2 ]*3 ] * 10 ``` this gives me a vector of ten vectors, with three [] elements in it: ``` [[[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []]] ``` that is , if I want to access parameters[0][0][1] I am out of bounds, whereas I want a dimension 2 for the innermost vectors along the third dimension. then I thought of this ``` [ [ [[] * 2] ]*3 ] * 10 ``` I was thinking that `[[] * 2]` would now bring me the thing I want, an innermost two elements vector. I obtain ``` [[[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]], [[[]], [[]], [[]]]] ``` So, how to do it, or how to escape this initialization? Kd rgds.

Original source