Prev: co-simulation
Next: how to unload an image from an axis
From: alice on 28 Apr 2010 01:10 I have two equations which are dependent on eachother in a loop . All these variables are matrices. I'd like to keep track of previous values so i can have a running sum throughout the execution of the loop. Any suggestions? thanks. for i=0:90 p=[1,2,3:6,4,2]; some intial value p0=[5,3,6:5,3,2] %i need the sum of all previous p for this step w=p-psum? p=p+w end
From: Darren Rowland on 28 Apr 2010 01:30 Try rearranging some of the code, so perhaps p0 = [5 3 6; 5 3 2]; psum = p0; p = [1 2 3; 6 4 2]; for i = 1:90 w = p - psum; psum = psum + p; p = p + w; end Darren
From: alice on 28 Apr 2010 01:55 On Apr 28, 1:30 am, "Darren Rowland" <darrenjremovethisrowl...(a)hotmail.com> wrote: > Try rearranging some of the code, so perhaps > > p0 = [5 3 6; 5 3 2]; > psum = p0; > > p = [1 2 3; 6 4 2]; > > for i = 1:90 > w = p - psum; > psum = psum + p; > p = p + w; > end > > Darren Thanks for the above comments. This is basically what my code is doing now. I guess I'm not sure if psum is a sum of all previous ps of just the current and previous p. I'll go through the number to make sure. A
From: alice on 28 Apr 2010 21:46 I think I need to preallocate and array which has dimensions which will change inside with loop so I can keep track of all the previous ps as they are created. So I would define the array before the loop, sort of like p = ones(X,Y, Z), Z will increase inside the loop based on i so I end up with i amoung of X by Y arrays. Not sure if I need two separate loops or not. Any advise on this general sort of method would be welcome. Thanks.
From: Darren Rowland on 29 Apr 2010 02:49
Alice, Looking at your equations, it doesn't matter if p is given as a 2x3 array or as a 6x1 vector. Just to keep the indexing easier, I would have p in the form of a vector then the history of p can be recorded in a matrix of dimension 6-by-i, i.e. pMat = zeros(6,i); % preallocate storage for p vectors then within the loop you will have pMat(:,i+1) = pMat(:,i) + w; Hth Darren |