可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!
回答1:
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:
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])
回答2:
You could try something like:
a = np.hstack(([0.2],np.linspace(1,60,60),[60.8]))
回答3:
np.concatenate([[.2], linspace(1,60,60), [60.8]])
回答4:
Does arange(0.2,60.8,0.2)
do what you want?
http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html
回答5:
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.
回答6:
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.
回答7:
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]
回答8:
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.)
回答9:
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)]