Tutorial

How to Get the Number of Elements in a Matrix in MATLAB

The function numel returns the number of elements in a matrix.

Preparation

Create a matrix using the following code:

A = [1, 2, 3; 4, 5, 6];

Syntax

n = numel(A) stores in n the number of elements in matrix A.

% Get the number of elements in matrix A
n = numel(A);

Further reading

I recommend the following books to learn more on matrix manipulation in MATLAB.

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a 2017 Textbook Excellence Award. The second chapter is dedicated to matrix manipulation and includes topics like matrix creation and indexing, and how to perform a wide list of operations on matrices.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Matrix manipulation is explained in the fourth chapter.

I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.

doc numel;

Source code

I hope you have learned how to get the number of elements in a matrix in MATLAB. The source code developed in this tutorial is available at this page.

Share

Your friends may need this knowledge to complete a task. Share this tutorial.