array math question

kcwallace

Centurion
Joined
May 27, 2004
Messages
175
Location
Austin, TX
Does anyone know of a method to calculate the average and standard deviation of a set of numbers in an array other than looping through each element and calculating the average, and then loop through the array for a second time to calculate the standard deviation?
 
Since you have to know the average before you can calculate the standard deviation, I don't think there is a clever way around this. Attempting to calculate as you go would only be a waste of operations. Unless you are talking about millions of elements, I wouldn't think there is much to worry about.

One thing you might be able to do to ease the pain, is calculate the sum of all elements as you add/remove them to/from the array. This would alleviate the need to iterate at all. But you will still need to calculate the mean before the standard deviation.
 
Back
Top