Re: how to calculate a running total of a vector [message #3213] |
Mon, 05 December 1994 02:33  |
sjt
Messages: 72 Registered: November 1993
|
Member |
|
|
Chris Chase S1A (chase@retro.jhuapl.edu) wrote:
: 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
I recall trying various things for this one a while back (on about
IDL2.0.13 or thereabouts, running on a VAX) and found the quickest was a
single loop:
if DA is your data array:
PS = fltarr(n_elements(DA))
ps(0)=da(0)
for j=1l,n_elements(da)-1 do ps(j)=ps(j-1)+da(j)
I know IDL loops are slow but the other methods tend to have so much
overhead of memory allocation etc that it just won. (That said, I haven't
tried Chris's method)
--
+------------------------+---------------------------------- --+---------+
| James Tappin, | School of Physics & Space Research | O__ |
| sjt@star.sr.bham.ac.uk | University of Birmingham | -- \/` |
| "If all else fails--read the instructions!" | |
+----------------------------------------------------------- --+---------+
|
|
|