How To Declare A 2 Dimensional Array With Different Row Lengths Using Np.array?
For example, I want a 2 row matrix, with a first row of length 1, and second row of length 2. I could do, list1 = np.array([1]) list2 = np.array([2,3]) matrix = [] m
Solution 1:
A matrix is by definition a rectangular array of numbers. NumPy does not support arrays that do not have a rectangular shape. Currently, what your code produces is an array, containing a list (matrix
), containing two more arrays.
array([array([1]), array([2, 3])], dtype=object)
I don't really see what the purpose of this shape could be, and would advise you simply use nested lists for whatever you are doing with this shape. Should you have found some use for this structure with NumPy however, you can produce it much more idiomatically like this:
>>> np.array([list1,list2])
array([array([1]), array([2, 3])], dtype=object)
Post a Comment for "How To Declare A 2 Dimensional Array With Different Row Lengths Using Np.array?"