开发者

help!for function

I have post the similar question before,but this time the problem is different,I got stuck with the following code..can anyone help with it?thanks in advance

I have fixed mu code as suggested,thanks

from numpy import *
#vs,fs,rs are all m*n matrixs,got initial values in,i.e vs[0],fs[0],rs[0] are known
#want use this foor loop to update them
vs=zeros((10,3))
vs[0]=([1,2,3])
fs=zeros((10,3))
fs[0]=([2,3,4])
vs=zeros((10,3))
vs[0]=([3,4,5])
for i in range(5):
    #start looping..
    vs[i+1]=vs[i]+fs[i]
    fs[i+1]=(rs[i]-re[i])
    rs[i+1]=rs[i]+vs[i]
print vs,fs,rs

then this code gives vs,fs,rs in different i,but not update each rows of rs,fs,vs and return me a single array of rs,fs,vs (fully updated). wha开发者_如何学Gots the problem here?..what should I add?thanks


Put your inizialization outside the loop! Right now, you're resetting the arrays to all zeros each time through the loop, over and over, which is absurd. You also appear to have a typo -- you set vs twice and rs never -- so I've tried to guess what you meant.

from numpy import *

#vs,fs,rs are all m*n matrixs,got initial values in,i.e vs[0],fs[0],rs[0] are known
#want use this foor loop to update them
vs=zeros((10,3))
vs[0]=([1,2,3])
fs=zeros((10,3))
fs[0]=([2,3,4])
rs=zeros((10,3))
rs[0]=([3,4,5])

for i in range(5):
    #start looping..
    vs[i+1]=vs[i]+fs[i]
    fs[i+1]=rs[i]-re[i]
    rs[i+1]=rs[i]+vs[i]
    print vs,fs,rs


You do not start looping where the comment indicates it, but at the for i in range(5): line. Everything in the indented block (the "body of the for-loop") is done repeatedly for each i. So setting vs and fs to zero is done repeatedly, each time deleting what was calculated before. These initializations should be done before the for.

Also vs is initialized twice while rs isn't initialized at all, probably that's a typo and it should look like this:

vs=zeros((10,3))
vs[0]=([1,2,3])
fs=zeros((10,3))
fs[0]=([2,3,4])
rs=zeros((10,3))
rs[0]=([3,4,5])

for i in range(5):
  #start looping..
  ...


I don't know exactly what you want to print. If you want to print every matrix every time it is updated, then you're fine. But if you want to print the matrices after all the updates have been completed, then you should bring that print statement out of the for loop.

This and what Alex and sth have said should fix your code fully

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜