MATLAB: Average of matrix element

average of matrix element

Suppose i have a 4X1 matrix like [a;b;c;d]. Now i want the output as [a;(a+b)/2;(a+b+c)/3;(a+b+c+d)/4].
How to do that?

Best Answer

x = [a;b;c;d];
cumsum(x) ./ (1:numel(x))