0

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). whats the problem here?..what should I add?thanks

asked Nov 15, 2009 at 18:27
0

3 Answers 3

2

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
answered Nov 15, 2009 at 18:30
Sign up to request clarification or add additional context in comments.

2 Comments

Alex is right. Initialization need to be outside the loop. It is resetting your matrices every time and this is hurting you.. A LOT.
It is hard to go wrong when you start your comment with "Alex is right". :)
0

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..
 ...
answered Nov 15, 2009 at 18:42

Comments

0

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

answered Nov 15, 2009 at 19:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.