计算累计平均值(均值) [英] Calculate cumulative average (mean)

查看:1149
本文介绍了计算累计平均值(均值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何计算某些数字的累计平均值.我将举一个简单的例子来描述我要寻找的东西.

I would like to know how to calculate the cumulative average for some numbers. I will give a simple example to describe what I am looking for.

我有以下数字

vec <- c(1, 2, 3, 4, 5)

如果我对这些数字进行平均,那么我将得到3.

If I do the average of these numbers I will get 3 as a result.

现在,如何计算这些数字的累计平均值.

Now, how to do the cumulative average of these numbers.

推荐答案

与列表的累加总和类似,我建议这样做: 向量 x 的累积平均值 avg 将包含从第1个位置到位置 i 的平均值.

In analogy to the cumulative sum of a list I propose this: The cumulative average avg of a vector x would contain the averages from 1st position till position i.

一种方法是通过将所有先前值相加并除以它们的数量来计算每个位置的平均值.

One method is just to compute the the mean for each position by summing over all previous values and dividing by their number.

通过将算术平均值的定义重写为递归公式.一得到

By rewriting the definition of the arithmetic mean as a recursive formula. One gets

avg(1) = x(1)

avg(i) = (i-1)/i*avg(i-1) + x(i)/i;    (i > 1)

对向量的每个元素(或列表,一维数组或您称之为的一维元素)求该表达式都会得到累计平均值.

Evaluating this expression for every element of your vector (or list, one-dimensional array or however you call it) gives you the cumulative average.

如果必须计算非常大或非常多的整数的平均值,则此递归方法会派上用场;如果必须存储它们的累加和,则会遇到溢出问题.

This recursive method comes in handy if you have to calculate an average over very large or very many integers and would run into an overflow if you had to store their cumulative sum.

在您的示例中

1, 2, 3, 4, 5

我们得到

1, 1.5, 2, 2.5, 3

这篇关于计算累计平均值(均值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆