cumsum
.. cumsum(A, [dim])
Cumulative sum along a dimension ``dim`` (defaults to 1).
See also :func:`cumsum!` to use a preallocated output array,
both for performance and to control the precision of the
output (e.g. to avoid overflow).
Examples
The cumsum
function in Julia calculates the cumulative sum along a specified dimension of an array. The default dimension is 1. Here are some examples of its usage:
-
Cumulative sum of a 1-dimensional array:
julia> A = [1, 2, 3, 4, 5]; julia> cumsum(A) 5-element Array{Int64,1}: 1 3 6 10 15
This example calculates the cumulative sum of the elements in the array
A
along dimension 1. -
Cumulative sum of a 2-dimensional array along a specified dimension:
julia> A = [1 2 3; 4 5 6; 7 8 9]; julia> cumsum(A, 2) 3×3 Array{Int64,2}: 1 3 6 4 9 15 7 15 24
It calculates the cumulative sum along dimension 2 of the 2-dimensional array
A
. - Cumulative sum of a vector of floating-point numbers:
julia> A = [1.2, 2.3, 3.4, 4.5]; julia> cumsum(A) 4-element Array{Float64,1}: 1.2 3.5 6.9 11.4
This example demonstrates the cumulative sum calculation on a vector of floating-point numbers.
It is worth mentioning the cumsum!
function, which can be used to perform the cumulative sum operation in-place for improved performance and control over precision.
See Also
abs2, beta, binomial, ceil, cell, cross, ctranspose, ctranspose!, cummin, cumprod, cumprod!, cumsum, cumsum!, cumsum_kbn, div, divrem, eigfact, eigfact!, eigmin, eps, erf, erfc, erfcinv, erfcx, erfi, erfinv, exp, exp10, exp2, expm1, exponent, factor, factorial, factorize, floor, gcd, invmod, log, log10, log1p, log2, logspace, max, min, mod, mod1, modf, next, nextpow, nextprod, num, primes, primesmask, prod, realmin, sqrt, sum!, sumabs, sumabs!, sumabs2, sumabs2!,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.