mean
mean of all values, or means along a given dimension
Syntax
y = mean(x) y = mean(x, orientation)
Arguments
- x
- Vector, matrix, or hypermatrix of real or complex numbers. Sparse matrices accepted.
- orientation
index of the dimension along which the mean is computed. It can be either
- a character
"*"
(default),"r"
,"c"
or"m"
. "m" is equivalent tofind(size(x)>1,1)
. - a positive integer. 1 is equivalent to "r" and 2 is equivalent to "c".
- a character
- y
- dense scalar if
orientation
is not used or ifx
is a scalar. Otherwise, array such thatsize(y,orientation)
is 1 (sparse-encoded ifx
is so).
Description
y = mean(x) or y = mean(x, "*") returns the mean of all entries. If at least one entry is NaN, NaN is returned. This scalar result is always dense-encoded.
y = mean(x,1) or y=mean(x,"r")
computes the means accross rows. y
is a row if
x
is a matrix.
y = mean(x,2) or y=mean(x,"c")
computes the means accross columns. y
is a column if
x
is a matrix.
y = mean(x, n) with 3 ≤ n ≤ ndims(x)
computes the means along the nth dimension of x
.
y = mean(x,'m') is the mean along the first non singleton dimension of x (for compatibility with Matlab).
mean([]) and mean(sparse([])) return NaN.
For any orientation not "m",
mean([], orientation) returns [] , and
mean(sparse([]), orientation) returns sparse([]) . |
mean() can be overloaded. |
Examples
With a matrix:
A = [0,1,1,0,1;1,0,0,1,1;0,0,1,0,0;0,0,1,0,0] mean(A) mean(A, 'r') mean(A, 'c')
--> A = [0,1,1,0,1;1,0,0,1,1;0,0,1,0,0;0,0,1,0,0] A = 0. 1. 1. 0. 1. 1. 0. 0. 1. 1. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. --> mean(A) ans = 0.4 --> mean(A, 'r') ans = 0.25 0.25 0.75 0.25 0.5 --> mean(A, 'c') ans = 0.6 0.6 0.2 0.2
With an hypermatrix:
A = [1,0,0,1,0,0,1,1,1,1,1,1,0,1,0,1,1,0,1,1,1,1,1,0,1,0,1,0,1,1,1,0,1,0,0,1,0,1,1,0]; A = matrix(A, [4,5,2]) mean(A) mean(A, 'r') mean(A, 'c') mean(A, 3) A = matrix(1:12, [1,1,2,3,2]); // in this case mean(A,'m') is equivalent to mean(A,3), the first non singleton dimension of A mean(A, 'm')
--> A = matrix(A, [4,5,2]) A = (:,:,1) 1. 0. 1. 0. 1. 0. 0. 1. 1. 0. 0. 1. 1. 0. 1. 1. 1. 1. 1. 1. (:,:,2) 1. 1. 1. 1. 0. 1. 0. 1. 0. 1. 1. 1. 1. 0. 1. 0. 0. 0. 1. 0. --> mean(A) ans = 0.625 --> mean(A, 'r') ans = (:,:,1) 0.5 0.5 1. 0.5 0.75 (:,:,2) 0.75 0.5 0.75 0.5 0.5 --> mean(A, 'c') ans = (:,:,1) 0.6 0.4 0.6 1. (:,:,2) 0.8 0.6 0.8 0.2 --> mean(A, 3) ans = 1. 0.5 1. 0.5 0.5 0.5 0. 1. 0.5 0.5 0.5 1. 1. 0. 1. 0.5 0.5 0.5 1. 0.5
See also
History
Version | Description |
6.0.1 | mean() is now overloadable. |
6.1.1 | Extension to sparse matrices. |
Report an issue | ||
<< harmean | Mean Central Tendency | meanf >> |