mean(v[, region])

Compute the mean of whole array v, or optionally along the dimensions in region. Note: Julia does not ignore NaN values in the computation. For applications requiring the handling of missing data, the DataArray package is recommended.


julia> xsum = rand(5)
julia> A = rand(3)
3-element Array{Float64,1}:
julia> mean(A)
  1. Compute the mean of a 1-dimensional array:

    julia> v = [1, 2, 3, 4, 5];
    julia> mean(v)

    This example calculates the mean of the array v, which is 3.0.

  2. Compute the mean along a specific dimension of a multi-dimensional array:

    julia> A = [1 2 3; 4 5 6; 7 8 9];
    julia> mean(A, dims = 1)
    1×3 Array{Float64,2}:
    4.0  5.0  6.0

    Here, the mean function is used to calculate the mean along the first dimension (columns) of the 2-dimensional array A. The result is a 1x3 array with the means of each column.

  3. Compute the mean along multiple dimensions:
    julia> B = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
    julia> mean(B, dims = (1, 2))
    1-element Array{Float64,1}:

    In this example, the mean function calculates the mean along both dimensions of the 2-dimensional array B. The result is a 1-element array with the overall mean of the array.

Common mistake example:

julia> v = []
julia> mean(v)
ERROR: MethodError: no method matching mean(::Array{Any,1})

Here, an empty array v is provided to the mean function, which results in a MethodError because Julia cannot determine the appropriate method to use. Make sure to provide a non-empty array to calculate the mean accurately.

See Also

cummax, eigmax, findmax, hist, hist!, hist2d, hist2d!, histrange, indmax, maxabs, maxabs!, maximum!, mean, mean!, median, median!, minabs, minabs!, minimum!, minmax, quantile!, realmax, std, stdm,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: