
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Mean Function
In MATLAB, the mean function is a powerful tool that helps you find the average or mean value of a set of numbers. Whether you're analyzing data, working with matrices, or simply calculating the average of a list of values, the mean function can save you time and effort.
Let us understand how to use the mean function by learning its syntax and executing a few examples.
Syntax
Following are the syntax for mean() function −
M = mean(A); M = mean(A, "all"); M = mean(A, dim);
The detailed explanation of the syntax is as follows −
A − This is where you input the data you want to find the mean of. It can be a vector, a matrix, even a single number or multidimensional array.
M − The mean function returns the average of the input data, and this is stored in the M variable.
"all" − The string "all" specifies that you want to calculate the mean of all elements in the array A, regardless of its size and dimensions.
dim − The dim argument specifies the dimension along which you want to calculate the mean. It can be an integer, either 1 or 2, corresponding to rows or columns in the case of a 2D matrix. For higher-dimensional arrays, you can specify a dimension index.
Examples of mean() Function
Following are the examples of using mean() function on vector, matrix and multidimensional array −
Example 1: Finding the Mean of a Vector
Suppose you have a list of exam scores: [85, 92, 78, 89, 95]. To calculate the mean score, you can use the mean function as follows −
scores = [85, 92, 78, 89, 95]; average_score = mean(scores)
When you execute above code in matlab command window the output is −
>> scores = [85, 92, 78, 89, 95]; average_score = mean(scores) average_score = 87.8000
Example 2: Mean of a given 2-D Matrix
If you have data stored in a matrix, such as a 2D array of values, you can still use the mean function to find the mean of the entire matrix. Here's an example −
data_matrix = [10, 20, 30; 40, 50, 60; 70, 80, 90]; average_value = mean(data_matrix, 'all')
The 'all' option tells MATLAB to calculate the mean of all elements in the matrix.
When you execute the same in matlab command window the output is −
>> data_matrix = [10, 20, 30; 40, 50, 60; 70, 80, 90]; average_value = mean(data_matrix, 'all') average_value = 50
Example 3: Calculating the Mean Along Rows and Columns of a Matrix
Let's say you have a matrix A representing student scores, with rows as students and columns as subjects −
A = [90, 85, 75; 78, 92, 88; 81, 89, 94];
To find the mean score for each student along columns, you can use:
A = [90, 85, 75; 78, 92, 88; 81, 89, 94]; M = mean(A, 2)
The output when you execute in matlab command window is −
>> A = [90, 85, 75; 78, 92, 88; 81, 89, 94]; M = mean(A, 2) M = 83.3333 86.0000 88.0000
To find the mean score for each subject along rows, you can use −
A = [90, 85, 75; 78, 92, 88; 81, 89, 94]; M = mean(A, 1)
On execution in matlab command window the output is −
>> A = [90, 85, 75; 78, 92, 88; 81, 89, 94]; M = mean(A, 1) M = 83.0000 88.6667 85.6667
Example 4: Calculating the Mean Along Multiple Dimensions
Suppose you have a 3D array A having random data as shown below −
A = rand(4, 3, 2); % Create a random 3D array
To find the mean along the first and second dimensions, you can use −
A = rand(4, 3, 2); M = mean(A, [1, 2])
When you execute the same in matlab command window the output is as follows −
>> A = rand(4, 3, 2); M = mean(A, [1, 2]) M(:,:,1) = 0.6139 M(:,:,2) = 0.6624