Find the mean vector of a Matrix
Last Updated :
05 Dec, 2022
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:
Let’s 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 .
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define rows 3
#define cols 3
void meanVector( int mat[rows][cols])
{
cout << "[ " ;
for ( int i = 0; i < rows; i++) {
double mean = 0.00;
int sum = 0;
for ( int j = 0; j < cols; j++)
sum += mat[j][i];
mean = sum / rows;
cout << mean << " " ;
}
cout << "]" ;
}
int main()
{
int mat[rows][cols] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
meanVector(mat);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int rows = 3 ;
static int cols = 3 ;
static void meanVector( int mat[][])
{
System.out.print( "[ " );
for ( int i = 0 ; i < rows; i++)
{
double mean = 0.00 ;
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( "]" );
}
public static void main (String[] args)
{
int mat[][] = {{ 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 }};
meanVector(mat);
}
}
|
Python3
rows = 3 ;
cols = 3 ;
def meanVector(mat):
print ( "[ " , end = "");
for i in range (rows):
mean = 0.00 ;
sum = 0 ;
for j in range (cols):
sum = sum + mat[j][i];
mean = int ( sum / rows);
print (mean, end = " " );
print ( "]" );
mat = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]];
meanVector(mat);
|
C#
using System;
class GFG
{
static int rows = 3;
static int cols = 3;
static void meanVector( int [,]mat)
{
Console.Write( "[ " );
for ( int i = 0; i < rows; i++)
{
double mean = 0.00;
int sum = 0;
for ( int j = 0; j < cols; j++)
sum += mat[j, i];
mean = sum / rows;
Console.Write(( int )mean + " " );
}
Console.Write( "]" );
}
public static void Main ()
{
int [,] mat = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
meanVector(mat);
}
}
|
PHP
<?php
$rows = 3;
$cols = 3;
function meanVector( $mat )
{
global $rows , $cols ;
echo "[ " ;
for ( $i = 0; $i < $rows ; $i ++)
{
$mean = 0.00;
$sum = 0;
for ( $j = 0; $j < $cols ; $j ++)
$sum += $mat [ $j ][ $i ];
$mean = $sum / $rows ;
echo $mean , " " ;
}
echo "]" ;
}
$mat = array ( array (1, 2, 3),
array (4, 5, 6),
array (7, 8, 9));
meanVector( $mat );
?>
|
Javascript
<script>
var rows = 3;
var cols = 3;
function meanVector(mat)
{
document.write( "[ " );
for ( var i = 0; i < rows; i++)
{
var mean = 0.00;
var sum = 0;
for ( var j = 0; j < cols; j++)
sum += mat[j][i];
mean = sum / rows;
document.write(mean + " " );
}
document.write( "]" );
}
var mat = [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ];
meanVector(mat);
</script>
|
Time Complexity: O(m * n), where m and n are the numbers of rows and columns respectively.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...