Array of arrays (Python/NumPy)

前端 未结 4 1452
北荒
北荒 2021-01-01 12:56

I am using Python/NumPy, and I have two arrays like the following:

array1 = [1 2 3]
array2 = [4 5 6]

And I would like to create a new array

4条回答
  •  一生所求
    2021-01-01 13:22

    It seems strange that you would write arrays without commas (is that a MATLAB syntax?)

    Have you tried going through NumPy's documentation on multi-dimensional arrays?

    It seems NumPy has a "Python-like" append method to add items to a NumPy n-dimensional array:

    >>> p = np.array([[1,2],[3,4]])
    
    >>> p = np.append(p, [[5,6]], 0)
    
    >>> p = np.append(p, [[7],[8],[9]],1)
    
    >>> p
    array([[1, 2, 7], [3, 4, 8], [5, 6, 9]])
    

    It has also been answered already...

    From the documentation for MATLAB users:

    You could use a matrix constructor which takes a string in the form of a matrix MATLAB literal:

    mat("1 2 3; 4 5 6")
    
    or
    
    matrix("[1 2 3; 4 5 6]")
    

    Please give it a try and tell me how it goes.

提交回复
热议问题