Variance and standard | variance function declaration Function for calculating mean ; Calculating sum ; Returning mean ; Function for calculating variance ; subtracting mean from elements ; a [ i ] [ j ] = fabs ( a [ i ] [ j ] ) ; squaring each terms ; taking sum ; declaring and initializing matrix ; for mean ; for variance ; for standard deviation ; displaying variance and deviation
< ? php function mean ( $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ sum += $ a [ $ i ] [ $ j ] ; return floor ( ( int ) $ sum / ( $ n * $ n ) ) ; } function variance ( $ a , $ n , $ m ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ a [ $ i ] [ $ j ] -= $ m ; $ a [ $ i ] [ $ j ] *= $ a [ $ i ] [ $ j ] ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ sum += $ a [ $ i ] [ $ j ] ; return floor ( ( int ) $ sum / ( $ n * $ n ) ) ; } $ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; $ m = mean ( $ mat , 3 ) ; $ var = variance ( $ mat , 3 , $ m ) ; $ dev = sqrt ( $ var ) ; echo " Mean : " ▁ , ▁ $ m ▁ , ▁ " " , STRNEWLINE TABSYMBOL " Variance : " ▁ , ▁ $ var ▁ , STRNEWLINE TABSYMBOL " " , ▁ " Deviation : " floor ( $ dev ) , " STRNEWLINE " ; ? >