473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Anyone have a mean, median, mode, stddev routines in C#??

I'm looking for mean, median, mode and standard deviation - maybe a couple
methods that take in an array of floats or something??

Thanks much!
Nov 16 '05 #1
2 12946
Drebin:

This is untested, but feel free to use as you see fit. For more extensive
statistical/math computations like Bessel functions, likelihood estimators,
and so forth, you might want to check out NMath. I've favored readability
over raw speed here, but this should be very fast.

HTH,
- k^2

///// Begin code.
using System;
using System.Collections;

public class MathAverages
{
// Computes the median x~ of a set of values X. O(1) implementation..
public static float Median(float[] values)
{
int size = values.Length - 1;
return ((values[size/2] == values[size/2 + 1]) / 2);
}

// Computes the mode set x{} of a set of values X. O(n) implementation.
public static float[] Mode(float[] values)
{
Hashtable h = new Hashtable();
int count = 0;
foreach (float value in values)
{
h[value]++;
if (h[value] > count)
count = h[value];
}

ArrayList modeValues = new ArrayList();
foreach (DictionaryEntry e in h)
{
if (e.Value >= count) modeValues.Add(e.Key);
}

return (float[])modeValues.ToArray(typeof(System.Single));
}

// Computes the arithmetic mean x-bar of a set of values X. O(n)
implementation.
public static float Mean(float[] values)
{
float sum = 0;
foreach (float value in values)
{
sum += value;
}
return sum/(values.Length);
}
}
///// End code.
"Drebin" <th*******@hotmail.com> wrote in message
news:nq*****************@newssvr33.news.prodigy.co m...
I'm looking for mean, median, mode and standard deviation - maybe a couple
methods that take in an array of floats or something??

Thanks much!

Nov 16 '05 #2
hehehe - excellent!!! Thanks much Kyle!! :-)
"Kyle Kaitan" <kkaitan [at] gmail [dot] com> wrote in message
news:e9**************@TK2MSFTNGP15.phx.gbl...
Drebin:

This is untested, but feel free to use as you see fit. For more extensive
statistical/math computations like Bessel functions, likelihood
estimators,
and so forth, you might want to check out NMath. I've favored readability
over raw speed here, but this should be very fast.

HTH,
- k^2

///// Begin code.
using System;
using System.Collections;

public class MathAverages
{
// Computes the median x~ of a set of values X. O(1) implementation..
public static float Median(float[] values)
{
int size = values.Length - 1;
return ((values[size/2] == values[size/2 + 1]) / 2);
}

// Computes the mode set x{} of a set of values X. O(n) implementation.
public static float[] Mode(float[] values)
{
Hashtable h = new Hashtable();
int count = 0;
foreach (float value in values)
{
h[value]++;
if (h[value] > count)
count = h[value];
}

ArrayList modeValues = new ArrayList();
foreach (DictionaryEntry e in h)
{
if (e.Value >= count) modeValues.Add(e.Key);
}

return (float[])modeValues.ToArray(typeof(System.Single));
}

// Computes the arithmetic mean x-bar of a set of values X. O(n)
implementation.
public static float Mean(float[] values)
{
float sum = 0;
foreach (float value in values)
{
sum += value;
}
return sum/(values.Length);
}
}
///// End code.
"Drebin" <th*******@hotmail.com> wrote in message
news:nq*****************@newssvr33.news.prodigy.co m...
I'm looking for mean, median, mode and standard deviation - maybe a
couple
methods that take in an array of floats or something??

Thanks much!


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: michael way | last post by:
I read the follow query about calculating median posted by Daivd Porta on 10/8/03. CREATE TABLE SomeValues (keyx CHAR(1) PRIMARY KEY, valuex INTEGER NOT NULL) INSERT INTO SomeValues VALUES...
2
by: Hugo L. | last post by:
I really don't know how to calculate the median. Can anybody help me?
2
by: Bob | last post by:
I have been looking at the code for MedianFind(pDte As String) from the following thread from UtterAccess.com: "Finding Median average grouped by field" I have been able to get it to run using...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
0
by: AZUCAR110 | last post by:
hi guys i need a program that median,frecuency, mode in a list o sorted numbers
3
by: Scott | last post by:
I need to take the median from a field of records in a report. Can someone shed the light how to do it. Thanks, Scott
4
by: Polar | last post by:
Hello everyone! I'm new here. I am doing a project, Digital Compass Navigation Aids. It consists of the 1490 Digital Compass, a P18F4620 Microcontroller, ISD2560 voice record/playback chip LM4808M...
1
by: shgu0501 | last post by:
hi I'm supposed to do a frequency array and find mode,median and count, but without storing the actual data in an array. I am able to build a frequency array but I can't return the mode since I am...
3
by: mehwishobaid | last post by:
i dont know wat is wrong with my code. when i compile. i get the error saying line 29: error: expression must have pointer-to-object type #include <iostream> using namespace std; #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.