Click here to Skip to main content
Licence CPOL
First Posted 20 Mar 2007
Views 18,131
Downloads 227
Bookmarked 16 times

This class has some nice statistical functions

By | 20 Mar 2007 | Article
It has some Statistical function such as mean , variance,...

Introduction

The class which is attached helps you to use statistical functions in your projects

It include these functinos :

1-Max

2-Min

3-Median

4-Mode

5-Mean

6-Variance

7-Covariance

8-VarCovarMatrix

9-Correlation

Using the code

Using the class is too easy.
Just try it. then enjoy it.

Something Nice

Have you ever tried to write a function that could calculate the Mode ?

It was really good experince i had writting this class

if you look at the function ( Mode ) you will find out, that it could count how many times an elemnt in an matrix of any type! with any dimension ( just less than 3) was repeated!

and finally it find the most repeated elements and return them as the Mode.

this is the code :

Public Shared Function Mode(ByVal InArray As Array) As Object()


        Dim Dimentions() As Integer = size(InArray)
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim NewInArray(InArray.Length - 1) As Object

        Select Case Dimentions.Length
            Case 1
                Array.Copy(InArray, NewInArray, InArray.Length)

            Case 2
                For i = 0 To Dimentions(0) - 1
                    For j = 0 To Dimentions(1) - 1
                        NewInArray(i * Dimentions(1) + j) = InArray(i, j)
                    Next
                Next

            Case 3
                For i = 0 To Dimentions(0) - 1
                    For j = 0 To Dimentions(1) - 1
                        For k = 0 To Dimentions(2) - 1
                            NewInArray(i * Dimentions(1) * Dimentions(2) + j * Dimentions(2) + k) = InArray(i, j, k)
                        Next
                    Next
                Next

            Case Else
                MsgBox("Matrix dimention must be less than 3", MsgBoxStyle.Critical, "Statistic")
                Return Nothing
        End Select

        Array.Sort(NewInArray)
        Dim Temp As Integer
        Dim Counter As Integer = 0
        Dim ModeArray(0) As ArrayValueRepetation

        i = 0

        While i < NewInArray.Length
            Temp = 0
            ReDim Preserve ModeArray(Counter)
            For j = i To NewInArray.Length - 1
                If NewInArray(i) = NewInArray(j) Then
                    Temp += 1
                    ModeArray(Counter).RepeatTimes = Temp
                    ModeArray(Counter).Value = NewInArray(i)
                ElseIf Temp = 1 Then
                    ModeArray(Counter).RepeatTimes = Temp
                    ModeArray(Counter).Value = NewInArray(i)
                    Exit For
                Else
                    Exit For
                End If
            Next
            Counter += 1
            i = j
        End While

        Dim RepeatTimes(Counter - 1) As Integer
        For i = 0 To Counter - 1
            RepeatTimes(i) = ModeArray(i).RepeatTimes
        Next

        Dim MaxRepeat As UInteger = Max(RepeatTimes)
        Dim OutVector(0) As Object
        Dim NumberOfModes As Integer = 0
        For i = 0 To Counter - 1
            If ModeArray(i).RepeatTimes = MaxRepeat Then
                ReDim Preserve OutVector(NumberOfModes)
                'OutVector.SetValue(ModeArray(i).Value, NumberOfModes)
                OutVector(NumberOfModes) = ModeArray(i).Value
                NumberOfModes += 1
            End If
        Next

        Return OutVector
    End Function
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Hossein Narimani Rad

Engineer

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionShould you have tried a different approach maybe? PinmemberDJBrown13:30 14 Jan '11  
AnswerRe: Should you have tried a different approach maybe? Pinmemberhossein narimani rad19:12 14 Jan '11  
Generalspelling PinmemberGwareth5:43 16 Apr '07  
AnswerRe: spelling Pinmemberhossien narimani0:07 25 Apr '07  
GeneralExceptions PinmemberKidan9:40 20 Mar '07  
AnswerRe: Exceptions Pinmemberhossien narimani6:22 31 Mar '07  
Generalbooo Pinmemberbug menot8:48 20 Mar '07  
AnswerRe: booo Pinmemberhossien narimani6:23 31 Mar '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 20 Mar 2007
Article Copyright 2007 by Hossein Narimani Rad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid