numpy array creating with a sequence

后端 未结 9 1335
陌清茗
陌清茗 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:06

    Have a look at np.r_. It's basically equivalent to what everyone else has suggested, but if you're coming from matlab, it's a bit more intuitive (and if you're coming from any other language, it's a bit counter-intuitive).

    As an example, vector=[0.2,1:60,60.8]; translates to:

    vector = np.r_[0.2, 1:61, 60.8]
    
    0 讨论(0)
  • 2020-12-15 21:09

    if I understand the matlab correctly, you could accomplish something like this using:

    a=np.array([0.2]+list(range(1,61))+[60.8])
    

    But there's probably a better way...the list(range(1,61)) could just be range(1,61) if you're using python 2.X.

    This works by creating 3 lists and then concatenating them using the + operator.

    The reason your original attempt didn't work is because

    a=[ [0.2], np.linspace(1,60,60), [60.8] ] creates a list of lists -- in other words:

    a[0] == [0.2] #another list (length 1)
    a[1] == np.linspace(1,60,60) #an array (length 60)
    a[2] == [60.8] #another list (length 1)
    

    The array function expects an iterable that is a sequence, or a sequence of sequences that are the same length.

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

    Does arange(0.2,60.8,0.2) do what you want?

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html

    0 讨论(0)
提交回复
热议问题