Re: how to calculate a running total of a vector [message #3215 is a reply to message #3213] |
Sun, 04 December 1994 08:54  |
VUKOVIC
Messages: 6 Registered: December 1994
|
Junior Member |
|
|
In <CHASE.94Dec2163443@retro.jhuapl.edu> chase@retro.jhuapl.edu 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?
>
>
> 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|
> :*)
>
Tried your suggestion, and it works. But it is damn slow compared to a simple
loop algorithm (.4 vs 4 sec for a 1024 length vector on an old vaxstation):
FUNCTION RSUMV,V
;+
; Produces a running total of a vector where result(i) is total(v(0:i))
;-
nn=n_elements(v)
res=fltarr(nn)
res(0)=v(0)
for ii=1,nn-1 do res(ii)=res(ii-1)+v(ii)
return,res
end
My final thought is to go maybe fia the FFT. One can maybe approximate the sum
by an integral, and do the integration fia FFT.
FFT(FFT(V,-1)/W,1)
where W is a frequency like vector. I may try that later.
Mirko
Mirko Vukovic | vukovic@uwmfe.neep.wisc.edu
Dept. of Nucl. Eng. and Eng. Phys. | mvukovic@wiscmacc.bitnet
U of Wisconsin -- Madison | phone: (608) 265-4069
1500 Johnson Drive; Madison WI 53706| fax: (608) 265-2364
|
|
|