Variance and standard | Java program to find mean and variance of a matrix . ; 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 ; Driver Code ; declaring and initializing matrix ; for mean ; for variance ; for standard deviation ; displaying variance and deviation
import java . io . * ; class GFG { static int mean ( int a [ ] [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += a [ i ] [ j ] ; return sum / ( n * n ) ; } static int variance ( int a [ ] [ ] , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { a [ i ] [ j ] -= m ; a [ i ] [ j ] *= a [ i ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += a [ i ] [ j ] ; return sum / ( n * n ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int m = mean ( mat , 3 ) ; int var = variance ( mat , 3 , m ) ; double dev = ( int ) Math . sqrt ( var ) ; System . out . println ( " Mean : ▁ " + m ) ; System . out . println ( " Variance : ▁ " + var ) ; System . out . println ( " Deviation : ▁ " + ( int ) dev ) ; } }