我想用矩阵存储所有For Loop值(),
但我不知道该怎么做。
感谢您的评论〜
import numpy as np
T1 = 3
T3 = 10
ydd = 0;
wdd = 0;
tt = 0;
t=0
for t in np.arange (t,T3,0.001) :
if t <= T1:
yd = 1/2*amax*(t**2)
wd = amax*t
ad = amax
elif t > T1:
yd = amax*(t**6)
wd = amax*t
ad = amax
答案 0 :(得分:0)
import numpy as np
T1 = 3
T3 = 10
ydd = 0;
wdd = 0;
tt = 0;
## declare arrays to store the data
## size of arrays is bit tricky.
## I am just calculating the number of times you are going to run the for loop.
## The array has to be same size
t=0
i=0
yd = np.zeros( int((T3-t)*1.0/0.001) )
wd = np.zeros( int((T3-t)*1.0/0.001) )
ad = np.zeros( int((T3-t)*1.0/0.001) )
for t in np.arange (t,T3,0.001) :
if t <= T1:
yd[i] = 1/2*amax*(t**2)
wd[i] = amax*t
ad[i] = amax
elif t > T1:
yd[i] = amax*(t**6)
wd[i] = amax*t
ad[i] = amax
i = i+1
## store in the array instead of variable
您现在可以访问yd,wd和ad数组中的所有值 免责声明:在不运行代码的情况下发布,因为我不确定amax是什么。
答案 1 :(得分:0)
没有for循环这样做。它的速度要快得多(尤其是 numpy!),并且对于没有相互依赖性的计算更自然/可读。
vals = np.arange(t, T3, 0.001)
cond = vals <= T1
yd = np.zeros_like(vals)
yd[cond] = (0.5 * amax * vals**2)[cond]
yd[!cond] = (amax * vals**6)[!cond]
wd = amax * vals
ad = np.full_like(vals, amax)