JIYIK CN >

Current Location:Home > Learning > PROGRAM > MATLAB >

Use mean() function in Matlab to get the average value of an array

Author:JIYIK Last Updated:2025/04/18 Views:

This tutorial will discuss finding the mean or average of an array using the function in MATLAB mean().


Use the MATLAB mean()function to find the average of an array

To find the mean of an array, we can use Matlab's inbuilt function mean(). If we pass a vector or array, mean()the function will return the average of all the elements in the array. For example, let's mean()find the mean of a vector using the function. See the code below.

vect = [1 3 5];
averg = mean(vect)

Output:

averg =

     3

If the input is a matrix, then mean()the function will return a row vector containing the mean of each matrix column. For example, suppose you have a matrix with three columns. Then mean()the function will return a row vector with three elements which will be the mean of each column. For example, let's use mean()the function to find the mean of the matrix columns. See the code below.

vect = [1 3 5; 2 3 6]
averg = mean(vect)

Output:

vect =

     1     3     5
     2     3     6


averg =

    1.5000    3.0000    5.5000

As you can see, the input matrix has three columns and the output has three elements, one for each column. If you don't want to take the mean of the columns, you want to take the mean of the rows. You can mean()specify this as the second argument in the function. You have to add the second argument, which will be an integer 2. For example, let's use mean()the function to find the mean of the rows of a matrix. See the code below.

vect = [1 3 5; 2 3 6]
averg = mean(vect,2)

Output:

vect =

     1     3     5
     2     3     6


averg =

    3.0000
    3.6667

As you can see, the input matrix has two rows and the output has two elements, one for each row. You can also specify the output file type or the data type of the output as mean()the second argument in the function. For example, you can specify that the output should be double or native. For example, let's define the data type of the above matrix as double. See the code below.

vect = [1 3 5; 2 3 6]
averg = mean(vect,'double')

Output:

vect =

     1     3     5
     2     3     6


averg =

    1.5000    3.0000    5.5000

If you don't want to use mean()the function, you can also use the sum()and length()functions to find the average. We know that the average is equal to the sum of the elements divided by the number of elements. We can use sum()the function to get the sum of the elements and length()the function to get the number of elements, and then we need to divide the sum by the length to get the average.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Commenting multiple lines in MATLAB

Publish Date:2025/04/18 Views:115 Category:MATLAB

This tutorial discusses how to comment multiple lines of code in MATLAB using the comment block method and the MATLAB Editor. Use comment blocks in MATLAB to comment multiple lines of code To comment one or two lines of code, we can % do it

MATLAB sorts the rows

Publish Date:2025/04/18 Views:137 Category:MATLAB

This tutorial will discuss the use of the function in MATLAB sortrows() to sort the rows present in a matrix. In data analysis and processing, sorting is essential as it makes the data easy to analyze and process when it is sorted. For exam

如何在 Matplotlib Pyplot 中显示网格

Publish Date:2024/02/04 Views:145 Category:Python

本文演示了如何在 Python Matplotlib 中在一个图上画一个网格。使用 grid()函数来绘制网格,并解释了如何改变网格颜色和线条类型。

如何在 Matplotlib 中画一条任意线

Publish Date:2024/02/04 Views:168 Category:Python

本教程讲解了我们如何在 Matplotlib 中使用 matplotlib.pyplot.plot()、matplotlib.pyplot.vlines()、matplotlib.pyplot.hlines()方法和 matplotlib.collection.LineCollection 绘制任意线条。

Matplotlib 中的叠加条形图

Publish Date:2024/02/04 Views:185 Category:Python

本教程展示了如何使用 plt.bar()方法将某些数据集的条形图堆叠在另一个数据集上。我们在 Matplotlib 中使用 matplotlib.pyplot.bar()方法生成条形图。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial