Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - English


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 to find(size(x)>1,1).
  • a positive integer. 1 is equivalent to "r" and 2 is equivalent to "c".

y
dense scalar if orientation is not used or if x is a scalar. Otherwise, array such that size(y,orientation) is 1 (sparse-encoded if x 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

  • sum — sum of array elements
  • median — median (row median, column median,...) of vector/matrix/array entries
  • stdev — standard deviation (row or column-wise) of vector/matrix entries

History

VersionDescription
6.0.1 mean() is now overloadable.
6.1.1 Extension to sparse matrices.
Report an issue
<< harmean Mean Central Tendency meanf >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:13:09 CEST 2024