EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Average
 

Matlab Average

Updated March 14, 2023

Matlab Average

 

 

Introduction to Matlab Average

Average, in statistics and mathematics, is computed by taking the sum of any group of objects or values and dividing the sum by several objects or values. Average is also referred to as ‘mean’. Average of any data provides us with the idea of the central tendency, i.e. it helps us in getting an intuition about a typical value in the data set. In MATLAB we use the ‘mean’ function to find the average.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

For example, if the ages of people in a group of 5 are 22, 26, 34, 27, and 45, then the average age is given by (22 + 26 +34 + 27 + 45) / 5 = 30.8

Syntax:

Below is the syntax of Matlab Average:

A = mean (M)

Explanation: A = mean(M) will return the average of all the elements of the array M. For matrix M, A = mean(M) will return the average of every column in M, in the form of a row vector

Examples to Implement Matlab Average

Let us now understand the code of mean function in MATLAB using different examples:

Example #1

In this example, we will take a 2 x 2 matrix and will find its average using the mean function.

For our first example, we will follow the following steps:

1. Create the 2 x 2 matrix

2. Pass the input matrix as an argument to the mean function

Code

M = [0   5; 3   6;] A = mean (M)

Output:

Matlab Average1

Explanation: First, Creating the 2 x 2 matrix. Passing the matrix ‘M’ as an input to the mean function. The mean function will find the average of elements in each column and will return a 1 x 2-row vector. Mathematically, the averages of elements of columns 1 and 2 are 1.5 and 5.5 respectively. As we can see in the output, we have obtained the average of column 1 as 1.5 and column 2 as 5.5, which is the same as expected by us.

Example #2

In this example, we will take a 3 x 3 matrix and will find its average using the mean function.

For this example, we will follow the following steps:

1. Create the 3 x 3 matrix

2. Pass the input matrix as an argument to the mean function

Code:

M = [3  5  9; 3   2  1; -4  5  8] A = mean (M)

Output:

Matlab Average2

Explanation: First, Creating the 3 x 3 matrix. Passing the matrix ‘M’ as an input to the mean function. The mean function will find the average of elements in each column and will return a 1 x 3-row vector. Mathematically, the averages of elements of columns 1, 2, and 3 are 0.6667, 4, 6 respectively. As we can see in the output, we have obtained the average of column 1 as 0.6667, column 2 as 4, and column 3 as 6, which is the same as expected by us.

In the above 2 examples, the average was calculated along each column because by default, this is how the mean function works. Next, we will learn how to find the average along the rows of a matrix using the mean function.

Example #3

In this example, we will take a 2 x 2 matrix and will find its average along the rows, using the mean function.

For this example, we will follow the following steps:

1. Create the 2 x 2 matrix.

2. Pass the input matrix as the first argument to the mean function.

3. Pass the second argument as ‘2’.

Code:

M = [5   2; 4   11;] A = mean (M, 2)

Output:

Pass the input

Explanation: First, Creating the 2 x 2 matrix Passing the matrix ‘M’ as the first argument to the mean function. The second argument ‘2’ is passed to ensure that the mean is calculated along the rows of the matrix. The mean function will find the average of elements in each row and will return a 2 x 1-row vector. Mathematically, the averages of elements of rows 1 and 2 are 3.5 & 7.5 respectively. As we can see in the output, we have obtained the average of row 1 as 3.5 and row 2 as 7.5, which is the same as expected by us.

Example #4

In this example, we will take a 3 x 3 matrix and will find its average along the rows, using the mean function.

For this example, we will follow the following steps:

1. Create the 3 x 3 matrix

2. Pass the input matrix as the first argument to the mean function

3. Pass the second argument as ‘2’

Code:

M = [2   4   6; 4    5   11; 5   6   1] A = mean (M, 2)

Output:

mean function

Explanation: First, Creating the 3 x 3 matrix. Passing the matrix ‘M’ as the first argument to the mean function. The second argument ‘2’ is passed to ensure that the mean is calculated along the rows of the matrix. The mean function will find the average of elements in each row and will return a 3 x 1-row vector. Mathematically, the averages of elements of rows 1, 2, and 3 are 4, 6.6667 & 4 respectively. As we can see in the output, we have obtained the average of row 1 as 4, row 2 as 6.6667, and row 3 as 4, which is the same as expected by us.

Conclusion

‘mean’ function is used in MATLAB to find the average of a matrix or an array. By default, the mean function computes the average along with the columns in the input matrix. We can pass a second argument as ‘2’ if we need the average along the rows of the matrix

Recommended Articles

This is a guide to Matlab Average. Here we discuss an introduction to Matlab Average, syntax, examples with code, output, and explanation. You can also go through our other related articles to learn more –

  1. MATLAB Exponential
  2. Ceil Matlab
  3. Matlab Plot Colors
  4. MATLAB Derivative

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW