meanMean or average of elements in an array
M = mean(A)A is a scalar, M is equal to A.A is a vector, M is equal to the mean of elements of A.A is a matrix or a multidimensional array, an element of M is equal to the mean of elements of the corresponding vector of A along its first non-singleton dimension.A is empty, it returns an empty array. M = mean(A, k)M corresponds to a vector of A along its k-th dimension.k-th dimension has zero length, then it returns an empty array.💡 If a vector contains
NaN, the mean of its elements given bymeanwill beNaN.
mean is fairly accurate according to the relative error. The error can be further reduced by using more samples, say, 10,000.% Anonymous function for calculating relative error in percentage
relative_error = @(x) abs(x - 30) / 30 * 100;
% Observations
r = exprnd(30,1,1000);
relative_error(mean(r))
% Smaller error by using more samples
r = exprnd(30,1,10000);
relative_error(mean(r))
ans =
2.3736
ans = 1e-1 ×
6.5230