image thumbnail
from cumulativ statistic by Mauro
cumstat calculate the mean and std of an array from 1 to n

cumstat(x,dim)
function [mx, sx] = cumstat(x,dim)

% [mx, sx] = nancumstat(x,dim)
% 
% [mx, sx] = nancumstat(x) calculate the mean and std of an array as seen below:
%   mx(n,:) = mean(1:n,:)
%   sx(n,:) = std(1:n,:)
%
% dim = 1 --> row wise
% dim = 2 --> col wise
%
% data arrays with NaN's please use nancumstat!

if nargin <2, dim = 1;end
mx = NaN(size(x));
sx = NaN(size(x));

switch dim
    case 1
        x1 = repmat((1:size(x,dim))',1,size(x,2));
        x2 = cumsum(x);
        x3 = cumsum((x).^2);
        mx = x2./x1;
        sx = sqrt((x1.*x3-x2.^2)./(x1.*(x1-1)));
    case 2
        x1 = repmat((1:size(x,dim)),size(x,1),1);
        x2 = cumsum(x,2);
        x3 = cumsum((x).^2,2);
        mx = x2./x1;
        sx = sqrt((x1.*x3-x2.^2)./(x1.*(x1-1)));
    otherwise
end

Contact us at files@mathworks.com