numpy array creating with a sequence

后端 未结 9 1333
陌清茗
陌清茗 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 20:47
    np.concatenate([[.2], linspace(1,60,60), [60.8]])
    
    0 讨论(0)
  • 2020-12-15 20:51

    You could try something like:

    a = np.hstack(([0.2],np.linspace(1,60,60),[60.8]))
    
    0 讨论(0)
  • 2020-12-15 20:51

    Just want to point out for any other people going from MATLAB to Numpy that you can construct an np.r_ array with colons and then use it to index

    E.g., if you have in matlab

    arr_ones = ones(10,10)
    

    Or in Numpy

    arr_ones = np.ones([10,10])
    

    You could in Matlab take only columns 1 through 5 as well as 7 like this:

    arr_ones(:,[1:5 7])
    

    Doing the same in Numpy is not (at least for me) intuitive. This will give you an "invalid syntax" error:

    arr_ones[:,[1:5,7]]
    

    However this works:

    inds = np.r[1:5,]
    arr_ones[:,inds]
    

    I know this is not technically a new answer, but using a colon to construct an array when indexing into a matrix seems so natural in Matlab, I am betting a lot of people that come to this page will want to know this. (I came here instead of asking a new question.)

    0 讨论(0)
  • 2020-12-15 20:53

    I somehow like the idea of constructing these segmented ranges you mentioned. If you use them alot, maybe a small function like

    import numpy as np
    
    def segrange(*args):
        result = []
        for arg in args:
            if hasattr(arg,'__iter__'):
                result.append(range(*arg))
            else:
                result.append([arg])
        return np.concatenate(result)
    

    that gives you

    >>> segrange(1., (2,5), (5,10,2))
    [ 1.  2.  3.  4.  5.  7.  9.]
    

    would be nice to have. Although, I would probably go for the answer using concatenate/hstack.

    0 讨论(0)
  • 2020-12-15 21:00

    Easiest way using numpy.repeat() ||| numpy.tile()

    a = np.array([1,2,3,4,5])
    
    np.r_[np.repeat(a,3),np.tile(a,3)]
    
    0 讨论(0)
  • 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])
    
    0 讨论(0)
提交回复
热议问题