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:
我正在从MATLAB过渡到scipy(+numpy)+matplotlib。我在执行某些事情时总是会遇到问题。我想在三个不同的部分创建一个简单的向量数组。在MATLAB中,我会做一些类似的事情:
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:
这导致一个一维数组的62个位置。我试着用scipy来实现它。我现在最接近的是:
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:
我认为我的主要障碍是我不知道如何在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!
numpy。我知道如何在数组中访问位置,虽然不是在创建序列时。任何帮助都将被感激,谢谢!
8 个解决方案
#1
13
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:
NumPy实现了MATLAB的数组创建函数,向量,使用两个函数而不是一个函数——每一个都隐式地指定了一个特定的轴,连接应该发生。这些函数是:
-
r_ (row-wise concatenation) and
一点r_(行操作连接)
-
c_ (column-wise)
c_(列)
So for your example, the NumPy equivalent is:
对于你的例子,NumPy等价的是:
>>> 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:
尽管如果一个虚数作为第三个参数,切片表示法就像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:
否则,它的行为就像arange:
>>> v = NP.r_[.2, 1:25:7, 60.8]
>>> v
array([ 0.2, 1. , 8. , 15. , 22. , 60.8])
#2
5
You could try something like:
你可以试试:
a = np.hstack(([0.2],np.linspace(1,60,60),[60.8]))
#3
3
np.concatenate([[.2], linspace(1,60,60), [60.8]])
#4
3
Does arange(0.2,60.8,0.2)
do what you want?
arange(0.2,60.8,0.2)做你想做的吗?
http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html
http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html
#5
2
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.
我很高兴。不过,我可能会使用concatenate/hstack来回答这个问题。
#6
1
if I understand the matlab correctly, you could accomplish something like this using:
如果我正确理解了matlab,你可以用以下方法来完成:
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.
但也许有更好的方法……如果您使用的是python 2.X,那么这个列表(范围(1,61))可以是范围(1,61)。
This works by creating 3 lists and then concatenating them using the +
operator.
通过创建3个列表,然后使用+运算符连接它们。
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=[[2], np.linspace(1,60,60)[60.8]]创建列表列表,换句话说:
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
1
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).
看一下np.r_。它基本上相当于其他人的建议,但如果你是从matlab中来的,它会更直观一些(如果你来自其他语言,这有点违反直觉)。
As an example, vector=[0.2,1:60,60.8];
translates to:
作为一个例子,向量=(0.2,一60,60.8);翻译:
vector = np.r_[0.2, 1:61, 60.8]
#8
1
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
我想指出的是,任何其他人从MATLAB到Numpy都可以构建一个np。带有冒号的r_数组,然后使用它来索引。
E.g., if you have in matlab
如果你有matlab的话。
arr_ones = ones(10,10)
Or in Numpy
或者在Numpy
arr_ones = np.ones([10,10])
You could in Matlab take only columns 1 through 5 as well as 7 like this:
你可以在Matlab中只取1到5和7这样的列
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:
在Numpy中做同样的事情并不(至少对我来说)是直觉的。这将给您一个“无效语法”错误:
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.)
我知道这在技术上不是一个新答案,但在Matlab中使用冒号来构造一个数组,这看起来是很自然的,我打赌很多来到这个页面的人都想知道这个。(我来这里不是问一个新问题。)
#1
13
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:
NumPy实现了MATLAB的数组创建函数,向量,使用两个函数而不是一个函数——每一个都隐式地指定了一个特定的轴,连接应该发生。这些函数是:
-
r_ (row-wise concatenation) and
一点r_(行操作连接)
-
c_ (column-wise)
c_(列)
So for your example, the NumPy equivalent is:
对于你的例子,NumPy等价的是:
>>> 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:
尽管如果一个虚数作为第三个参数,切片表示法就像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:
否则,它的行为就像arange:
>>> v = NP.r_[.2, 1:25:7, 60.8]
>>> v
array([ 0.2, 1. , 8. , 15. , 22. , 60.8])
#2
5
You could try something like:
你可以试试:
a = np.hstack(([0.2],np.linspace(1,60,60),[60.8]))
#3
3
np.concatenate([[.2], linspace(1,60,60), [60.8]])
#4
3
Does arange(0.2,60.8,0.2)
do what you want?
arange(0.2,60.8,0.2)做你想做的吗?
http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html
http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html
#5
2
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.
我很高兴。不过,我可能会使用concatenate/hstack来回答这个问题。
#6
1
if I understand the matlab correctly, you could accomplish something like this using:
如果我正确理解了matlab,你可以用以下方法来完成:
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.
但也许有更好的方法……如果您使用的是python 2.X,那么这个列表(范围(1,61))可以是范围(1,61)。
This works by creating 3 lists and then concatenating them using the +
operator.
通过创建3个列表,然后使用+运算符连接它们。
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=[[2], np.linspace(1,60,60)[60.8]]创建列表列表,换句话说:
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
1
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).
看一下np.r_。它基本上相当于其他人的建议,但如果你是从matlab中来的,它会更直观一些(如果你来自其他语言,这有点违反直觉)。
As an example, vector=[0.2,1:60,60.8];
translates to:
作为一个例子,向量=(0.2,一60,60.8);翻译:
vector = np.r_[0.2, 1:61, 60.8]
#8
1
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
我想指出的是,任何其他人从MATLAB到Numpy都可以构建一个np。带有冒号的r_数组,然后使用它来索引。
E.g., if you have in matlab
如果你有matlab的话。
arr_ones = ones(10,10)
Or in Numpy
或者在Numpy
arr_ones = np.ones([10,10])
You could in Matlab take only columns 1 through 5 as well as 7 like this:
你可以在Matlab中只取1到5和7这样的列
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:
在Numpy中做同样的事情并不(至少对我来说)是直觉的。这将给您一个“无效语法”错误:
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.)
我知道这在技术上不是一个新答案,但在Matlab中使用冒号来构造一个数组,这看起来是很自然的,我打赌很多来到这个页面的人都想知道这个。(我来这里不是问一个新问题。)