numpy array creating with a sequence

后端 未结 9 1389
陌清茗
陌清茗 2020-12-15 20:34

I am on my transitional trip from MATLAB to scipy(+numpy)+matplotlib. I keep having issues when implementing some things. I want to create a simple vector array in three dif

9条回答
  •  遥遥无期
    2020-12-15 21:02

    Well NumPy implements MATLAB's array-creation function, vector, using two functions instead of one--each implicitly specifies a particular axis along which concatenation ought to occur. These functions are:

    • r_ (row-wise concatenation) and

    • c_ (column-wise)


    So for your example, the NumPy equivalent is:

    >>> import numpy as NP
    
    >>> v = NP.r_[.2, 1:10, 60.8]
    
    >>> print(v)
         [  0.2   1.    2.    3.    4.    5.    6.    7.    8.    9.   60.8]
    

    The column-wise counterpart is:

    >>> NP.c_[.2, 1:10, 60.8]
    

    slice notation works as expected [start:stop:step]:

    >>> v = NP.r_[.2, 1:25:7, 60.8]
    
    >>> v
      array([  0.2,   1. ,   8. ,  15. ,  22. ,  60.8])
    

    Though if an imaginary number of used as the third argument, the slicing notation behaves like linspace:

    >>> v = NP.r_[.2, 1:25:7j, 60.8]
    
    >>> v
      array([  0.2,   1. ,   5. ,   9. ,  13. ,  17. ,  21. ,  25. ,  60.8])
    


    Otherwise, it behaves like arange:

    >>> v = NP.r_[.2, 1:25:7, 60.8]
    
    >>> v
      array([  0.2,   1. ,   8. ,  15. ,  22. ,  60.8])
    

提交回复
热议问题