how to calculate a running total of a vector [message #3218] |
Fri, 02 December 1994 09:51  |
VUKOVIC
Messages: 6 Registered: December 1994
|
Junior Member |
|
|
Suppose I have a vector v.
I want to generate a vector vv whose i-th element is a sum of the first i
elements of v. How to do it fast -- without loops?
I thought of multiplying a matrix with 1's on and bellow its diagonal by v.
That would do it, but seems like a bit of overkill.
|
|
|
Re: how to calculate a running total of a vector [message #3223 is a reply to message #3218] |
Fri, 02 December 1994 13:34  |
chase
Messages: 62 Registered: May 1993
|
Member |
|
|
In article <3bnmri$e91@news.doit.wisc.edu> VUKOVIC@uwmfe.neep.wisc.edu (Mirko Vukovic) writes:
Mirko> Suppose I have a vector v. I want to generate a vector vv
Mirko> whose i-th element is a sum of the first i elements of v. How
Mirko> to do it fast -- without loops?
Mirko> I thought of multiplying a matrix with 1's on and bellow its
Mirko> diagonal by v. That would do it, but seems like a bit of
Mirko> overkill.
This will work, but it is overkill also. I probably does the same
amount of computations as your matrix multiply solution.
running_sum = (convol([0,v],replicate(1.,n_elements(v)),center=0,/edge_tru n))(1:*)
Chris Chase
--
===============================
Bldg 24-E188
The Applied Physics Laboratory
The Johns Hopkins University
Laurel, MD 20723-6099
(301)953-6000 x8529
chris.chase@jhuapl.edu
|
|
|