The module "cummean" of the Mastrave modelling library
Copyright and license notice of the function cummean
Copyright © 2005,2006,2007,2008,2009,2010,2011 Daniele de Rigo
The file cummean.m is part of Mastrave.
Mastrave is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Mastrave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Mastrave. If not, see http://www.gnu.org/licenses/.
Function declaration
answer = cummean( values , dim = [] , mode = 'a' )
Description
Utility to extend the function mean(.) providing cumulative means of the elements of the array values along a given dimension dim .
Input arguments
values ::numeric:: Vector, matrix or multi-dimensional array of numbers. dim ::scalar_index|empty:: Scalar positive integer representing the dimension along which the cumulative means have to be computed. If dim is an empty array [] , the dimension is the first non-singleton dimension. In case values is a vector, this definition means that the default dimension is the one along which the elements of the vector values are aligned. If omitted, the default value is []. mode ::string:: String declaring the kind of mean to be computed. If omitted, its default value is: 'a'. Valid modes are: mode │ meaning ─────────────────┼────────────────────────────────── 'a' │ Compute the ordinary arithmetic '--arithmetic' │ mean. ─────────────────┼────────────────────────────────── 'g' │ Compute the geometric mean. '--geometric' │ ─────────────────┼────────────────────────────────── 'h' │ Compute the harmonic mean. '--harmonic' │
Example of usage
% Basic usage % Vectors: m = ceil( rand(1,7)* 100 ) cm = cummean( m ) assert( cm(end) == mean(m) ) % Matrices: m = ceil( rand(5,7)* 100 ) cm = cummean( m ) assert( cm(end,:) == mean(m) ) % Passing a custom dimension cm = cummean( m , 2 ) assert( cm(:,end) == mean(m,2) ) % Dealing with multi-dimensional arrays m = ceil( rand(5,7,3)* 100 ) cm = cummean( m ) cm = cummean( m , [] ) assert( cm(end,:,:) == mean(m) ) cm = cummean( m , 3 ) assert( cm(:,:,end) == mean(m,3) ) % Passing the kind of mean to be computed cm = cummean( m , [], '--arithmetic' ) cm = cummean( m , [], 'a' ) assert( cm(end,:,:) == mean(m) ) % Geometric mean cm = cummean( m , [], '--geometric' ) cm = cummean( m , [], 'g' ) assert( cm(end,:,:) == prod(m,1).^(1/size(m,1)) ) cm = cummean( m , 2, 'g' ) assert( cm(:,end,:) == prod(m,2).^(1/size(m,2)) ) % Harmonic mean cm = cummean( m , [], '--harmonic' ) cm = cummean( m , [], 'h' ) assert( cm(end,:,:) == size(m,1)./sum(1./m,1) ) cm = cummean( m , 3, '--harmonic' ) assert( cm(:,:,end) == size(m,3)./sum(1./m,3) )
Memory requirements:
O( numel( values ) )
See also:
cumstd, cumvar, cumsumsq, groupfun
Keywords:
cumulative operators, scan
Version: 0.4.3
Support
The Mastrave modelling library is committed to provide reusable and general - but also robust and scalable - modules for research modellers dealing with computational science. You can help the Mastrave project by providing feedbacks on unexpected behaviours of this module. Despite all efforts, all of us - either developers or users - (should) know that errors are unavoidable. However, the free software paradigm successfully highlights that scientific knowledge freedom also implies an impressive opportunity for collectively evolve the tools and ideas upon which our daily work is based. Reporting a problem that you found using Mastrave may help the developer team to find a possible bug. Please, be aware that Mastrave is entirely based on voluntary efforts: in order for your help to be as effective as possible, please read carefully the section on reporting problems. Thank you for your collaboration.