Python for loop only saves the last loop in my 2-d array -
i producing 1 dimensional random walks , want loop save maximum distance origin has been reached far, time progresses. 5 random walks produced. code:
for j in range(5): r = rand(10000) t = range(10000) x = zeros(10000) y = zeros((10000, 5)) in range(10000): walk = r[i] if walk < 0.5: x[i] = x[i-1] - 1 y[:,j]= maximum.accumulate(abs(x)) else: x[i] = x[i-1] + 1 y[:,j]= maximum.accumulate(abs(x)) plot(t,x, label="walk %d" %(j+1)) title("1-d random walk (position versus time)") xlabel("time") ylabel("position") legend(loc="best") grid()
the problem after loop iterates on set range (5) output 2-d array includes last iteration. somehow, overwhites previous ones, 10000x5 array last row filled in.
how can make work?
you've, reason, chosen execute
y = zeros((10000, 5))
over , on again, each time through outer loop.
as obvious consequence, last assignment y
can possibly "take", evidently overriding previous assignment same name. other behavior possibly expect such repeated assignments?!
move assignment outside outer loop, , there no "overriding" of name y
.
Comments
Post a Comment