## 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.

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:**

**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:**

**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:**

**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:**

**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 –

3 Online Courses | 1 Hands-on Project | 8+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses