How to calculate or approximate the median of a list without storing the list

后端 未结 10 1190
你的背包
你的背包 2020-11-28 22:24

I\'m trying to calculate the median of a set of values, but I don\'t want to store all the values as that could blow memory requirements. Is there a way of calculating or ap

10条回答
  •  悲&欢浪女
    2020-11-28 22:41

    David's suggestion seems like the most sensible approach for approximating the median.

    A running mean for the same problem is a much easier to calculate:

    Mn = Mn-1 + ((Vn - Mn-1) / n)

    Where Mn is the mean of n values, Mn-1 is the previous mean, and Vn is the new value.

    In other words, the new mean is the existing mean plus the difference between the new value and the mean, divided by the number of values.

    In code this would look something like:

    new_mean = prev_mean + ((value - prev_mean) / count)
    

    though obviously you may want to consider language-specific stuff like floating-point rounding errors etc.

提交回复
热议问题