sum(A, dims)
sum(A, dims)
Sum elements of an array over the given dimensions.
Examples
-
Sum of an array:
julia> arr = [1, 2, 3, 4, 5]; julia> sum(arr) 15
This example calculates the sum of all elements in the array
arr
. -
Sum of a range of numbers:
julia> sum(1:100) 5050
It computes the sum of all numbers from 1 to 100 using a range.
- Sum of a matrix:
julia> matrix = [1 2 3; 4 5 6; 7 8 9]; julia> sum(matrix) 45
It calculates the sum of all elements in the matrix.
Common mistake example:
julia> str = "Hello";
julia> sum(str)
ERROR: MethodError: no method matching sum(::String)
In this example, the sum
function is called on a string (str
). However, sum
expects a collection of numbers, so it throws a MethodError
. Make sure to provide a collection of numbers to the sum
function for correct results.
See Also
Array, broadcast, cat, combinations, conj!, digits!, fieldnames, fill, fill!, last, length, maximum, minimum, ones, parent, parentindexes, partitions, permutations, pointer, pointer_to_array, promote_shape, rand!, reshape, scale, similar, sum, sum_kbn, takebuf_array, transpose!, vec, zeros,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.