cumsum!
cumsum!(B, A, [dim])
Cumulative sum of A
along a dimension, storing the result in B
. The dimension defaults to 1.
Examples
In the Julia programming language, the function cumsum!(B, A, [dim])
Computes the cumulative sum of array A
along the specified dimension dim
and stores the result in array B
. If dim
is not provided, the cumulative sum is computed along the first dimension by default.
julia> A = [1, 2, 3, 4];
julia> B = similar(A);
julia> cumsum!(B, A)
4-element Array{Int64,1}:
1
3
6
10
Common examples of its use:
-
Compute cumulative sum along the first dimension:
julia> A = [1, 2, 3, 4]; julia> B = similar(A); julia> cumsum!(B, A) 4-element Array{Int64,1}: 1 3 6 10
This example computes the cumulative sum of the elements in array
A
along the first dimension and stores the result inB
. - Compute cumulative sum along a specified dimension:
julia> A = [1 2 3; 4 5 6]; julia> B = similar(A); julia> cumsum!(B, A, 2) 2×3 Array{Int64,2}: 1 3 6 4 9 15
Here, the cumulative sum of
A
is computed along the second dimension (columns) and stored inB
.
Common mistake example:
julia> A = [1, 2, 3, 4];
julia> B = [0, 0, 0, 0, 0];
julia> cumsum!(B, A)
ERROR: DimensionMismatch("dimensions must match")
In this example, B
is not the same size as the cumulative sum result. It's crucial to ensure that the size of B
matches the expected size of the cumulative sum result to avoid this error.
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.