问题描述
我对我从MATLAB过渡之旅SciPy的(+ numpy的)+ matplotlib。我一直在实施一些东西的时候有问题。
我想创建三个不同的部分简单的矢量数组。在MATLAB我会做这样的事情:
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];
这导致在62位置的一维阵列。我想实现这个使用SciPy的。我现在最接近的是这样的:
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.
我相信我的主要障碍是,我无法弄清楚如何在MATLAB翻译这个简单的操作:
I believe my main obstacle is that I can't figure out how to translate this simple operation in MATLAB:
a=[1:2:20];
要numpy的。我知道该怎么做访问数组中的位置,创建序列虽然不是时候。
任何帮助将AP preciated,
谢谢!
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的实现MATLAB的数组创建功能的矢量的,使用的两个的功能,而不是one--每个隐含指定沿的特定轴线串联的应该发生。这些功能是:
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的等效是:
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])
不过,如果一个的虚数的用于作为第三个参数,切片符号的行为像 linspace 的
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])
这篇关于numpy的阵列的序列创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!