# Find the mean vector of a Matrix

Given a matrix of size M x N, the task is to find the Mean Vector of the given matrix.

Examples:

```Input : mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output : Mean Vector is [4 5 6]
Mean of column 1 is (1 + 4 + 7) / 3 = 4
Mean of column 2 is (2 + 5 + 8) / 3 = 5
Mean of column 3 is (3 + 6 + 9) / 3 = 6

Input : mat[][] =  {{2, 4},
{6, 8}}
Output : Mean Vector is [4 6]
Mean of column 1 is (2 + 6) / 2 = 4
Mean of column 2 is (4 + 8) / 2 = 6```

Approach:
Lets take a matrix mat of dimension 5×3 representing lengths, breadths, heights of 5 objects.
Now, the resulting mean vector will be a row vector of the following format :

`[mean(length) mean(breadth)  mean(height)]`

Note: If we have a matrix of dimension M x N, then the resulting row vector will be having dimension 1 x N
Now, simply calculate the mean of each column of the matrix which will give the required mean vector .

## C++

 `// C++ program to find mean vector ` `// of given matrix ` `#include ` `using` `namespace` `std; ` `#define rows 3 ` `#define cols 3 ` ` `  `// Function to find mean vector ` `void` `meanVector(``int` `mat[rows][cols]) ` `{ ` `    ``cout << ``"[ "``; ` ` `  `    ``// loop to traverse each column ` `    ``for` `(``int` `i = 0; i < rows; i++) { ` ` `  `        ``// to calculate mean of each row ` `        ``double` `mean = 0.00; ` ` `  `        ``// to store sum of elements of a column ` `        ``int` `sum = 0; ` ` `  `        ``for` `(``int` `j = 0; j < cols; j++) ` `            ``sum += mat[j][i]; ` ` `  `        ``mean = sum / rows; ` `        ``cout << mean << ``" "``; ` `    ``} ` ` `  `    ``cout << ``"]"``; ` `} ` ` `  `// Drivers code ` `int` `main() ` `{ ` ` `  `    ``int` `mat[rows][cols] = { { 1, 2, 3 }, ` `                            ``{ 4, 5, 6 }, ` `                            ``{ 7, 8, 9 } }; ` ` `  `    ``meanVector(mat); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find  ` `// mean vector of given matrix ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `static` `int` `rows = ``3``; ` `static` `int` `cols = ``3``; ` ` `  `// Function to  ` `// find mean vector ` `static` `void` `meanVector(``int` `mat[][]) ` `{ ` `    ``System.out.print(``"[ "``); ` ` `  `    ``// loop to traverse  ` `    ``// each column ` `    ``for` `(``int` `i = ``0``; i < rows; i++)  ` `    ``{ ` ` `  `        ``// to calculate mean ` `        ``// of each row ` `        ``double` `mean = ``0.00``; ` ` `  `        ``// to store sum of ` `        ``// elements of a column ` `        ``int` `sum = ``0``; ` ` `  `        ``for` `(``int` `j = ``0``; j < cols; j++) ` `            ``sum += mat[j][i]; ` ` `  `        ``mean = sum / rows; ` `        ``System.out.print((``int``)mean + ``" "``); ` `    ``} ` ` `  `    ``System.out.print(``"]"``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `mat[][] = {{``1``, ``2``, ``3``}, ` `                   ``{``4``, ``5``, ``6``}, ` `                   ``{``7``, ``8``, ``9``}}; ` ` `  `    ``meanVector(mat); ` `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67. `

## Python3

 `# Python3 program to find  ` `# mean vector of given  ` `# matrix ` `rows ``=` `3``; ` `cols ``=` `3``; ` ` `  `# Function to ` `# find mean vector ` `def` `meanVector(mat): ` `    ``print``(``"[ "``, end ``=` `""); ` ` `  `    ``# loop to traverse ` `    ``# each column ` `    ``for` `i ``in` `range``(rows): ` `         `  `        ``# to calculate ` `        ``# mean of each row ` `        ``mean ``=` `0.00``; ` ` `  `        ``# to store sum of  ` `        ``# elements of a column ` `        ``sum` `=` `0``; ` ` `  `        ``for` `j ``in` `range``(cols): ` `            ``sum` `=` `sum` `+` `mat[j][i]; ` ` `  `        ``mean ``=` `int``(``sum` `/``rows); ` `        ``print``(mean, end ``=` `" "``); ` ` `  `    ``print``(``"]"``); ` ` `  `# Driver Code ` `mat ``=` `[[``1``, ``2``, ``3``], ` `       ``[``4``, ``5``, ``6``], ` `       ``[``7``, ``8``, ``9``]]; ` ` `  `meanVector(mat); ` ` `  `# This code is contributed ` `# by mits `

## C#

 `// C# program to find  ` `// mean vector of given matrix ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `static` `int` `rows = 3; ` `static` `int` `cols = 3; ` ` `  `// Function to  ` `// find mean vector ` `static` `void` `meanVector(``int` `[,]mat) ` `{ ` `    ``Console.Write(``"[ "``); ` ` `  `    ``// loop to traverse  ` `    ``// each column ` `    ``for` `(``int` `i = 0; i < rows; i++)  ` `    ``{ ` ` `  `        ``// to calculate mean ` `        ``// of each row ` `        ``double` `mean = 0.00; ` ` `  `        ``// to store sum of ` `        ``// elements of a column ` `        ``int` `sum = 0; ` ` `  `        ``for` `(``int` `j = 0; j < cols; j++) ` `            ``sum += mat[j, i]; ` ` `  `        ``mean = sum / rows; ` `        ``Console.Write((``int``)mean + ``" "``); ` `    ``} ` ` `  `    ``Console.Write(``"]"``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main ()  ` `{ ` `    ``int``[,] mat = {{1, 2, 3}, ` `                  ``{4, 5, 6}, ` `                  ``{7, 8, 9}}; ` ` `  `    ``meanVector(mat); ` `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67. `

## PHP

 ` `

Output:

```[ 4 5 6 ]
```

Time Complexity:
O(rows * cols)

My Personal Notes arrow_drop_up Maths is the language of nature

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, Mithun Kumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.