Do a function that maintains moving average, the function gives the average of all the numbers that have been put in the function.
does anyone know how to do this ?
If list is an array of numbers of the form [a,b,c,...,z] then we can simply take
list
[a,b,c,...,z]
average = sum(list)/length(list)
If you prefer not to have numbers entered as an array, you can do this:
function y = average(varargin) list = cell2mat(varargin); y = sum(list)/length(list);
Moving average of n points: use conv:
n
conv
data = [1 2 4 6 8 11 5]; n = 3; result = conv(data, repmat(1/n, [1 n]), 'valid');
Cumulative average from the beginning to current position: use cumsum:
cumsum
result = cumsum(data)./(1:numel(data));
Copyright © 2021 JogjaFile Inc.
If
listis an array of numbers of the form[a,b,c,...,z]then we can simply takeIf you prefer not to have numbers entered as an array, you can do this: