Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Matlab用Python循环生成变量_Python_Loops_Numpy_For Loop_If Statement - Fatal编程技术网

从Matlab用Python循环生成变量

从Matlab用Python循环生成变量,python,loops,numpy,for-loop,if-statement,Python,Loops,Numpy,For Loop,If Statement,我正在尝试将一个程序从Matlab转换为Python 我用Python写了: age=np.arange(start_age, start_age+D, deltat) For num in age: if age[:]<(65): Y=1 break else Y=0 break break H = (1/r) * (1 - math.exp(-r * max(0, (65 - age[:]))) A = ((1 - theta)

我正在尝试将一个程序从Matlab转换为Python

我用Python写了:

age=np.arange(start_age, start_age+D, deltat)
For num in age:        
if age[:]<(65):
    Y=1
    break
else
    Y=0
    break
    break
H = (1/r) * (1 - math.exp(-r * max(0, (65 - age[:])))
A = ((1 - theta) * r - rho) / theta + 0.5 * ((1 - theta) / theta ** 2) *(    _lambda **2)
g = (1/A) * (math.exp(A * (D - (age[:] - start_age))) - 1)
age=np.arange(起始年龄,起始年龄+D,延迟)
对于年龄中的num:
如果年龄[:]请尝试以下方法:

for num in age:
   if num < 65:
     Y=1
     break
   else:
     Y=0
     break
H = (1/r) * (1 - math.exp(-r * max(0, (65 - age[:])))
A = ((1 - theta) * r - rho) / theta + 0.5 * ((1 - theta) / theta ** 2) *(    
_lambda **2)
g = (1/A) * (math.exp(A * (D - (age[:] - start_age))) - 1)# equation (A11)
年龄为num的
:
如果num<65:
Y=1
打破
其他:
Y=0
打破
H=(1/r)*(1-math.exp(-r*max(0,(65-age[:]))
A=((1-θ)*r-ρ)/θ+0.5*((1-θ)/θ**2)*(
_λ**2)
g=(1/A)*(数学实验(A*(D-(年龄[:]-开始年龄))-1)#方程(A11)
如果像这样使用for循环,那么可以使用列表中的evry元素num代替age

但是:

在使用喙的方式中,如果我弄错了,你总是会在数组的第一个元素处停下来纠正我,但我不认为这是你想要的…因此,或者你失去了中断,或者你可以像这样检查第一个元素:

if age[0] < 65:
如果年龄[0]<65岁:

听起来这就是你想要的
年龄
Y

In [314]: age = np.arange(20,80,5)
In [315]: Y = np.where(age<65, 1, 0)
In [316]: age
Out[316]: array([20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75])
In [317]: Y
Out[317]: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0])
math.exp
适用于标量值,
np.exp
适用于数组


通过有限的测试:

In [318]: H = (1-np.exp(max(0, 65-age)))
----------------------------------------------------------------------
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
哎呀,错了
max
。这就是Python标量:

In [319]: H = (1-np.exp(np.maximum(0, 65-age)))
In [320]: H
Out[320]: 
array([ -3.49342711e+19,  -2.35385267e+17,  -1.58601345e+15,
        -1.06864746e+13,  -7.20048993e+10,  -4.85165194e+08,
        -3.26901637e+06,  -2.20254658e+04,  -1.47413159e+02,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00])
In [321]: g = np.exp((1-(age-20)))
In [322]: g
Out[322]: 
array([  2.71828183e+00,   1.83156389e-02,   1.23409804e-04,
         8.31528719e-07,   5.60279644e-09,   3.77513454e-11,
         2.54366565e-13,   1.71390843e-15,   1.15482242e-17,
         7.78113224e-20,   5.24288566e-22,   3.53262857e-24])

此代码有许多问题。它没有正确缩进。它使用了几个未定义的变量。
for
应该是小写。
否则:
缺少冒号。为什么在
else
块中有2个
break
语句?而且,
H
a
g
都不是函数,它们是使用我前面提到的未定义变量的简单表达式赋值。谢谢!我仍在学习所有这些,我确实对numpy和not numpy的不同函数感到困惑。谢谢!
In [319]: H = (1-np.exp(np.maximum(0, 65-age)))
In [320]: H
Out[320]: 
array([ -3.49342711e+19,  -2.35385267e+17,  -1.58601345e+15,
        -1.06864746e+13,  -7.20048993e+10,  -4.85165194e+08,
        -3.26901637e+06,  -2.20254658e+04,  -1.47413159e+02,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00])
In [321]: g = np.exp((1-(age-20)))
In [322]: g
Out[322]: 
array([  2.71828183e+00,   1.83156389e-02,   1.23409804e-04,
         8.31528719e-07,   5.60279644e-09,   3.77513454e-11,
         2.54366565e-13,   1.71390843e-15,   1.15482242e-17,
         7.78113224e-20,   5.24288566e-22,   3.53262857e-24])