MATLAB Answers

Average of matrix element

1 view (last 30 days)
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?

  1 Comment

Jos (10584)
Jos (10584) on 8 Aug 2019
Answered! Great minds, as well as mine, think alike ;-)

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Aug 2019
x = [a;b;c;d];
cumsum(x) ./ (1:numel(x))

  0 Comments

Sign in to comment.

More Answers (3)

Adam
Adam on 8 Aug 2019
cumsum( [a; b; c; d] ) ./ ( 1:4 )';

  0 Comments

Sign in to comment.


madhan ravi
madhan ravi on 8 Aug 2019
cumsum(matrix)./(1:numel(matrix)) % where matrix is a column vector

  0 Comments

Sign in to comment.


Jos (10584)
Jos (10584) on 8 Aug 2019
One easy option
A = [10 20 30 40]
B = cumsum(A) ./ (1:numel(A))

  0 Comments

Sign in to comment.

Sign in to answer this question.