function WCUMSUM, A ;+ ;NAME: ; WCUMSUM ; ;PURPOSE: ; To sum all of the elements in an array or matrix. ; If A is a matrix, then sum(A) is a row vector with ; the sum over each previous element in the column. This ; function is an equivalent of Matlab's cumsum function. ; ;CATEGORY: ; Simple math. ; ;CALLING SEQUENCE: ; Result = WCUMSUM(A) ; ;INPUTS: ; A: A vector or matrix ; ;OUTPUTS: ; The cumulative sum of the vector or matrix. ; ;RESTRICTIONS: ; None. ; ;EXAMPLE: ; >a = [[8,2,6],[3,6,7],[4,9,3]] ; >print, a ; 8 2 6 ; 3 6 7 ; 4 9 3 ; >print, WCUMSUM(a) ; 8.000 2.000 6.000 ; 11.000 8.000 13.000 ; 15.000 17.000 16.000 ;EXAMPLE: ; >a = [1, 2, 3, 4 5, 6, 7, 8, 9, 10] ; >print, WCUMSUM(a) ; 1 3 6 10 15 21 28 36 45 55 ; ; ;MODIFICATION HISTORY: ; Amara Graps, September 1995 ; copyright (c) Amara Graps 1995, 1996. ;- t = SIZE(A) IF t(0) eq 1 THEN BEGIN ;1D array nn = N_ELEMENTS(a) csum = A*0 csum(0) = A(0) FOR i = 1, nn-1 DO BEGIN csum(i) = csum(i-1)+A(i) END ENDIF ELSE BEGIN ;Matrix numcol = t(1) ;number of columns numrow=t(2) ;number of rows csum = FLTARR(numcol, numrow) ;Step through the cols FOR i = 0, numcol-1 DO BEGIN FOR j = 0, numrow -1 DO BEGIN ;And sum up all previous values IF j eq 0 THEN BEGIN csum(i,j) = A(i,j) END ELSE BEGIN csum(i, j) = csum(i,j-1)+A(i,j) END ;if END ;j END ;i ENDELSE RETURN, csum END ;************************************************