average

Find average of collection of TimeSpans

a 夏天 提交于 2019-11-28 20:03:28
I have collection of TimeSpans, they represent time spent doing a task. Now I would like to find the average time spent on that task. It should be easy but for some reason I'm not getting the correct average. Here's my code: private TimeSpan? GetTimeSpanAverage(List<TimeSpan> sourceList) { TimeSpan total = default(TimeSpan); var sortedDates = sourceList.OrderBy(x => x); foreach (var dateTime in sortedDates) { total += dateTime; } return TimeSpan.FromMilliseconds(total.TotalMilliseconds/sortedDates.Count()); } vc 74 You can use the Average extension method : double doubleAverageTicks =

Calculate average in java

 ̄綄美尐妖づ 提交于 2019-11-28 12:37:12
EDIT: Ive written code for the average, but i dont know how to make it so that it also uses ints from my args.length rather than the array I need to write a java program that can calculate: 1. the number of integers read in 2. the average value—which need not be an integer! NOTE! i dont want to calculate the average from the array but the integers in the args. Currently i have written this: int count = 0; for (int i = 0; i<args.length -1; ++i) count++; System.out.println(count); } int nums[] = new int[] { 23, 1, 5, 78, 22, 4}; double result = 0; //average will have decimal point { for(int i=0;

calculate sending file speed/sec by taking the average of 5 times of sent bytes [duplicate]

≯℡__Kan透↙ 提交于 2019-11-28 12:35:36
问题 This question already has an answer here: Calculate speed per sec and time left of sending a file using sockets tcp c# 3 answers im trying to calculate the transfer file speed per second using the average i took the different between the sent bytes sum and the prevSum 5 times per second does the code below give me the correct speed? should i change the rate array size ? or should i change Thread.Sleep(value) ? im so confused because each time a change a little thing the speed value changes..

Average of grouped rows in Sql Server

混江龙づ霸主 提交于 2019-11-28 11:56:32
I have an Sql Server Table.. it's something like this: Id ...... Column1 ...... Column2 ```````````````````````````````` 1 ........ 1 ............. 34 2 ........ 1 ............. 44 3 ........ 2 ............. 45 4 ........ 2 ............. 36 5 ........ 2 ............. 23 6 ........ 3 ............. 68 7 ........ 3 ............. 26 So, I need to select the average of Column2,but group with column1 before doing that. I mean, if I say Avg(Column2) it just returns a single row with the average of all rows. What I need is, first i need to group them by column so: Average of column2 where column1 = 1

Counting and computing the average length of words in ruby

时光怂恿深爱的人放手 提交于 2019-11-28 11:48:12
问题 I'm trying to debug a program in ruby that is meant to compute and print the average length of words in an array. words = ['Four', 'score', 'and', 'seven', 'years', 'ago', 'our', 'fathers', 'brought', 'forth', 'on', 'this', 'continent', 'a', 'new', 'nation', 'conceived', 'in', 'Liberty', 'and', 'dedicated', 'to', 'the', 'proposition', 'that', 'all', 'men', 'are', 'created', 'equal'] word_lengths = Array.new words.each do |word| word_lengths << word_to.s end sum = 0 word_lengths.each do |word

Calculating moving average/stdev in SAS?

大兔子大兔子 提交于 2019-11-28 11:44:07
Hye guys, I included a screenshot to help clarify my problem: http://i40.tinypic.com/mcrnmv.jpg . I'm trying to calculate some kind of moving average and moving standard deviation. The thing is I want to calculate the coefficients of variation (stdev/avg) for the actual value. Normally this is done by calculating the stdev and avg for the past 5 years. However sometimes there will be observations in my database for which I do not have the information of the past 5 years (maybe only 3, 2 etc). That's why i want a code that will calculate the avg and stdev even if there is no information for the

Average time for datetime list

笑着哭i 提交于 2019-11-28 10:13:48
Looking for fastest solution of time averaging problem. I've got a list of datetime objects. Need to find average value of time (excluding year, month, day). Here is what I got so far: import datetime as dtm def avg_time(times): avg = 0 for elem in times: avg += elem.second + 60*elem.minute + 3600*elem.hour avg /= len(times) rez = str(avg/3600) + ' ' + str((avg%3600)/60) + ' ' + str(avg%60) return dtm.datetime.strptime(rez, "%H %M %S") Here's a better way to approach this problem Generate a sample of datetimes In [28]: i = date_range('20130101',periods=20000000,freq='s') In [29]: i Out[29]:

Calculate the average value of a mongodb document [duplicate]

三世轮回 提交于 2019-11-28 10:09:19
问题 This question already has an answer here: Mongo average aggregation query with no group 3 answers Suppose that I have a collection like this: { _id: 1, city: "New York", state: "NY", murders: 328 } { _id: 2, city: "Los Angeles", state: "CA", murders: 328 } ... The collection shows us the number of murders in all cities of USA. I'd like to calculate the average of murders in all the country. I tried to use $group: db.murders.aggregate([{$group: {_id:"$state", pop: {$avg:"$murders"} } }]) But I

Averaging over every n elements of a numpy array

牧云@^-^@ 提交于 2019-11-28 05:42:10
I have a numpy array. I want to create a new array which is the average over every consecutive triplet of elements. So the new array will be a third of the size as the original. As an example: np.array([1,2,3,1,2,3,1,2,3]) should return the array: np.array([2,2,2]) Can anyone suggest an efficient way of doing this? I'm drawing blanks. If your array arr has a length divisible by 3: np.mean(arr.reshape(-1, 3), axis=1) Reshaping to a higher dimensional array and then performing some form of reduce operation on one of the additional dimensions is a staple of numpy programming. 来源: https:/

Averaging every n elements of a vector in matlab

拥有回忆 提交于 2019-11-28 04:43:45
问题 I would like to average every 3 values of an vector in Matlab, and then assign the average to the elements that produced it. Examples: x=[1:12]; y=%The averaging operation; After the operation, y= [2 2 2 5 5 5 8 8 8 11 11 11] Therefore the produced vector is the same size, and the jumping average every 3 values replaces the values that were used to produce the average (i.e. 1 2 3 are replaced by the average of the three values, 2 2 2 ). Is there a way of doing this without a loop? I hope that