median
median(v[, region])
Compute the median of whole array v
, or optionally along the dimensions in region
. For even number of elements no exact median element exists, so the result is equivalent to calculating mean of two median elements. NaN
is returned if the data contains any NaN
values. For applications requiring the handling of missing data, the DataArrays
package is recommended.
Examples
julia> xsum = rand(5)
median(xsum)
0.6850542627340954
julia> A = rand(3)
3-element Array{Float64,1}:
0.722853
0.606612
0.846898
julia> median(A)
0.6557513931743997
-
Calculate the median of a 1D array:
julia> arr = [5, 10, 15, 20, 25]; julia> median(arr) 15.0
This example calculates the median of the array
arr
. -
Calculate the median along a specific dimension:
julia> matrix = [1 2 3; 4 5 6; 7 8 9]; julia> median(matrix, 1) 1×3 Array{Float64,2}: 4.0 5.0 6.0
It calculates the median along the first dimension (columns) of the matrix.
-
Handling arrays with NaN values:
julia> data = [3, 5, NaN, 7, 9]; julia> median(data) NaN
If the array contains NaN values, the result will be NaN.
- Calculate the median of a range of elements:
julia> arr = [10, 20, 30, 40, 50]; julia> median(view(arr, 2:4)) 30.0
This example calculates the median of a range of elements using a view of the array.
Common mistake example:
julia> arr = []
julia> median(arr)
ERROR: MethodError: no method matching median(::Array{Any,1})
In this example, an empty array is provided to the median
function, which results in a MethodError
. Make sure the array is not empty before calculating the median.
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.