Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy array creating with a sequence

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 different parts. In MATLAB I would do something like:

vector=[0.2,1:60,60.8];

This results in a one dimensional array of 62 positions. I'm trying to implement this using scipy. The closest I am right now is this:

a=[[0.2],linspace(1,60,60),[60.8]]

However this creates a list, not an array, and hence I cannot reshape it to a vector array. But then, when I do this, I get an error

a=array([[0.2],linspace(1,60,60),[60.8]])
ValueError: setting an array element with a sequence.

I believe my main obstacle is that I can't figure out how to translate this simple operation in MATLAB:

a=[1:2:20];

to numpy. I know how to do it to access positions in an array, although not when creating a sequence. Any help will be appreciated, thanks!

like image 924
lllllll Avatar asked May 25 '12 11:05

lllllll


People also ask

How do you create a sequence array in Python?

Creating Arrays from Python Sequences # a list of numbers will become a 1D-array >>> np. array([1., 2., 3.]) # shape: (3,) array([ 1., 2., 3.]) Nested lists/tuples will be used to construct multidimensional arrays.

How do I create a NumPy sequence?

Problem: How to create a sequence of linearly increasing values? Solution: Use NumPy's arange() function. The np. arange([start,] stop[, step]) function creates a new NumPy array with evenly-spaced integers between start (inclusive) and stop (exclusive).

How do I create a NumPy array with a specific shape?

Use the numpy. zeros() function to create a numpy array of a specified shape that is filled with the value zero (0). The numpy. zeros function is nearly the same as numpy.

How do I create a NumPy array with random values?

Using Numpy randint() function Using this function we can create a NumPy array filled with random integers values. This function returns an array of shape mentioned explicitly, filled with random integer values.


1 Answers

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])
like image 188
doug Avatar answered Sep 23 '22 04:09

doug