Average of matrix element

4 visualizzazioni (ultimi 30 giorni)
Supriya Gain
Supriya Gain il 8 Ago 2019
Commentato: Jos (10584) il 8 Ago 2019
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 Commento
Jos (10584)
Jos (10584) il 8 Ago 2019
Answered! Great minds, as well as mine, think alike ;-)

Accedi per commentare.

Risposta accettata

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

Più risposte (3)

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

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

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

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by