Variance and standard-deviation of a matrix

06 Apr, 2021
Prerequisite – Mean, Variance and Standard Deviation, Variance and Standard Deviation of an arrayGiven a matrix of size n*n. We have to calculate variance and standard-deviation of given matrix. Examples :  
Input : 1 2 3
        4 5 6
        6 6 6 
Output : variance: 3
         deviation: 1

Input : 1 2 3
        4 5 6
        7 8 9 
Output : variance: 6
         deviation: 2  
Explanation: First mean should be calculated by adding sum of each elements of the matrix. After calculating mean, it should be subtracted from each element of the matrix.Then square each term and find out the variance by dividing sum with total elements. Deviation: It is the square root of the variance.Example:  
      1 2 3
      4 5 6
      7 8 9
Here mean is 5 and variance is approx 6.66 
Below is code implementation: 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find mean and// variance of a matrix.#include <bits/stdc++.h>using namespace std; // variance function declarationint variance(int, int, int); // Function for calculating meanint mean(int a[][3], int n){    // Calculating sum    int sum = 0;    for (int  i = 0; i < n; i++)        for (int j = 0; j < n; j++)            sum += a[i][j];         // Returning mean    return sum / (n * n);} // Function for calculating varianceint variance(int a[][3], int n, int m){    int sum = 0;    for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++) {             // subtracting mean from elements            a[i][j] -= m;             // a[i][j] = fabs(a[i][j]);            // squaring each terms            a[i][j] *= a[i][j];        }    }     // taking sum    for (int i = 0; i < n; i++)        for (int j = 0; j < n; j++)            sum += a[i][j];        return sum / (n * n);} // driver programint main(){    // declaring and initializing matrix    int mat[3][3] = { { 1, 2, 3 },                      { 4, 5, 6 },                      { 7, 8, 9 } };     // for mean    int m = mean(mat, 3);     // for variance    int var = variance(mat, 3, m);     // for standard deviation    int dev = sqrt(var);     // displaying variance and deviation     cout << "Mean: " << m << "\n"          << "Variance: " << var << "\n"          << "Deviation: " << dev << "\n";    return 0;}
// Java program to find mean// and variance of a matrix.import java.io.*; class GFG{// Function for// calculating meanstatic int mean(int a[][],                int n){    // Calculating sum    int sum = 0;    for (int i = 0; i < n; i++)        for (int j = 0; j < n; j++)            sum += a[i][j];         // Returning mean    return sum / (n * n);} // Function for// calculating variancestatic 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++)        {             // subtracting mean            // from elements            a[i][j] -= m;             // a[i][j] = fabs(a[i][j]);            // squaring each terms            a[i][j] *= a[i][j];        }    }     // taking sum    for (int i = 0; i < n; i++)        for (int j = 0; j < n; j++)            sum += a[i][j];     return sum / (n * n);} // Driver Codepublic static void main (String[] args){ // declaring and// initializing matrixint mat[][] = {{1, 2, 3},               {4, 5, 6},               {7, 8, 9}}; // for meanint m = mean(mat, 3); // for varianceint var = variance(mat, 3, m); // for standard// deviationdouble dev = (int)Math.sqrt(var); // displaying variance// and deviationSystem.out.println("Mean: " + m);System.out.println("Variance: " +                            var);System.out.println("Deviation: " +                        (int)dev);}} // This code is contributed// by akt_mit
# Python3 program to find mean# and variance of a matrix.import math; # variance function declaration# Function for calculating meandef mean(a, n):     # Calculating sum    sum = 0;    for i in range(n):        for j in range(n):            sum += a[i][j];         # Returning mean    return math.floor(int(sum / (n * n))); # Function for calculating variancedef variance(a, n, m):    sum = 0;    for i in range(n):        for j in range(n):             # subtracting mean            # from elements            a[i][j] -= m;             # a[i][j] = fabs(a[i][j]);            # squaring each terms            a[i][j] *= a[i][j];     # taking sum    for i in range(n):        for j in range(n):            sum += a[i][j];     return math.floor(int(sum / (n * n))); # Driver Code # declaring and# initializing matrixmat = [[1, 2, 3],       [4, 5, 6],       [7, 8, 9]]; # for meanm = mean(mat, 3); # for variancevar = variance(mat, 3, m); # for standard deviationdev = math.sqrt(var); # displaying variance# and deviationprint("Mean:", m);print("Variance:", var);print("Deviation:", math.floor(dev)); # This code is contributed by mits
// C# program to find mean// and variance of a matrix.using System; class GFG{     // Function for// calculating meanstatic int mean(int [,]a,                int n){    // Calculating sum    int sum = 0;    for (int i = 0; i < n; i++)        for (int j = 0; j < n; j++)            sum += a[i, j];         // Returning mean    return sum / (n * n);} // Function for// calculating variancestatic 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++)        {             // subtracting mean            // from elements            a[i, j] -= m;             // a[i][j] = fabs(a[i][j]);            // squaring each terms            a[i, j] *= a[i, j];        }    }     // taking sum    for (int i = 0; i < n; i++)        for (int j = 0; j < n; j++)            sum += a[i,j];     return sum / (n * n);} // Driver Codestatic public void Main (){ // declaring and// initializing matrixint [,]mat = {{1, 2, 3},              {4, 5, 6},              {7, 8, 9}}; // for meanint m = mean(mat, 3); // for varianceint var = variance(mat, 3, m); // for standard deviationdouble dev = (int)Math.Sqrt(var); // displaying variance and deviationConsole.WriteLine("Mean: " + m );    Console.WriteLine("Variance: " + var);    Console.WriteLine("Deviation: " + dev);}} // This code is contributed by ajit
<?php// PHP program to find mean// and variance of a matrix. // variance function declaration// Function for calculating meanfunction mean($a, $n){    // Calculating sum    $sum = 0;    for ($i = 0; $i < $n; $i++)        for ( $j = 0; $j < $n; $j++)            $sum += $a[$i][$j];         // Returning mean    return floor((int)$sum / ($n * $n));} // Function for calculating variancefunction variance($a, $n, $m){    $sum = 0;    for ($i = 0; $i < $n; $i++)    {        for ($j = 0; $j < $n; $j++)        {             // subtracting mean            // from elements            $a[$i][$j] -= $m;             // a[i][j] = fabs(a[i][j]);            // squaring each terms            $a[$i][$j] *= $a[$i][$j];        }    }     // taking sum    for ($i = 0; $i < $n; $i++)        for ( $j = 0; $j < $n; $j++)            $sum += $a[$i][$j];     return floor((int)$sum / ($n * $n));} // Driver Code // declaring and// initializing matrix$mat = array(array(1, 2, 3),             array(4, 5, 6),             array(7, 8, 9)); // for mean$m = mean($mat, 3); // for variance$var = variance($mat, 3, $m); // for standard deviation$dev = sqrt($var); // displaying variance// and deviationecho "Mean: " , $m , "\n",     "Variance: " , $var ,      "\n", "Deviation: " ,        floor($dev) , "\n"; // This code is contributed by ajit?>
<script> // JavaScript program to find mean// and variance of a matrix // Function for// calculating meanfunction mean(a, n){    // Calculating sum    let sum = 0;    for (let i = 0; i < n; i++)        for (let j = 0; j < n; j++)            sum += a[i][j];           // Returning mean    return sum / (n * n);}   // Function for// calculating variancefunction variance(a, n, m){    let sum = 0;    for (let i = 0; i < n; i++)    {        for (let j = 0; j < n; j++)        {               // subtracting mean            // from elements            a[i][j] -= m;               // a[i][j] = fabs(a[i][j]);            // squaring each terms            a[i][j] *= a[i][j];        }    }       // taking sum    for (let i = 0; i < n; i++)        for (let j = 0; j < n; j++)            sum += a[i][j];       return sum / (n * n);} // Driver code // declaring and// initializing matrixlet mat = [[1, 2, 3],               [4, 5, 6],               [7, 8, 9]];   // for meanlet m = mean(mat, 3);   // for variancelet varr = variance(mat, 3, m);   // for standard// deviationlet dev = Math.sqrt(varr);   // displaying variance// and deviationdocument.write("Mean: " + Math.floor(m) + "<br/>");document.write("Variance: " +                            Math.floor(varr) + "<br/>");document.write("Deviation: " +                        Math.floor(dev) + "<br/>");                             // This code is contributed by code_hunt.                           </script>
Mean: 5
Variance: 6
Deviation: 2
 
This article is contributed by Himanshu Ranjan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
Mithun Kumar
code_hunt
statistical-algorithms
Mathematical
Matrix
Mathematical
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.