Written and maintained by CASRAI Editorial Board
Published
On this page
numel in MATLAB returns the total number of elements in an array: n = numel(A), which MathWorks documents as equal to prod(size(A)). Researchers and analysts use it to size loops, check that two data sets line up, and confirm an import read the expected number of values. This guide covers the syntax, worked examples with their outputs, how numel differs from length and size, the pitfalls with text, cell arrays and tables, and the free GNU Octave equivalent.
What numel is used for in research
Any time a script needs to know how many values a variable holds, numel gives one number regardless of whether the variable is a vector, a matrix or a multidimensional array. Typical uses are loop bounds (for k = 1:numel(A)), sanity checks after loading data, and sizing a result to match an input. The same count is available as prod(size(A)); the MathWorks reference lists prod and size under See Also. (Editorial note: the loop and sanity-check uses are common practice, not statements from the reference page.)
Install or access (official sources)
numel is a built-in MATLAB function. MATLAB itself is obtained from MathWorks at mathworks.com; check your institution’s licence arrangements there or with your IT office. If you have no licence, GNU Octave is a free alternative that provides its own numel (see the table below).
Step-by-step: count the elements of an array
- Create or load the variable, for example a 4-by-4 magic square with a second page added:
A = magic(4); A(:,:,2) = A'; - Call the function:
n = numel(A) - Read the result in the Command Window or Workspace:
n = 32(16 elements on each of 2 pages). - Cross-check if you wish:
prod(size(A))returns the same value, 32.
Source: MathWorks, numel reference page, read 2026-11-08.
Other input types from the reference page
- String array:
A = ["a" "b" "c"; "d" "e" "f"]; numel(A)returns 6. - Cell array:
A = {'dog','cat','fish','horse'}; numel(A)returns 4, one per cell. - Table: for a 5-row table with 4 variables, where one variable (BloodPressure) has two columns,
numel(A)returns 20, that is rows times variables.
Output and how to read it
The output is a single nonnegative whole number: the product of all dimension lengths. For the 4-by-4-by-2 example, 4 × 4 × 2 = 32. A scalar gives 1. The reference page states that A can be a scalar, vector, matrix, multidimensional array, table or timetable, and it lists support for tall arrays, C/C++ code generation with MATLAB Coder, GPU Coder, thread-based environments, and gpuArray and distributed arrays (Parallel Computing Toolbox).
numel vs length vs size vs ndims
These functions answer different questions about the same array. The numel and size rows follow the MathWorks reference; the length, ndims and size-argument rows follow the GNU Octave manual, whose definitions are given for Octave.
| Function | Question it answers | For the 4-by-4-by-2 example |
|---|---|---|
numel(A) |
How many elements in total (equals prod(size(A))) |
32 |
size(A) |
The length of each dimension | 4 4 2 |
size(A, dim) |
Length of one dimension; rows are dimension 1, columns dimension 2 (Octave manual) | size(A,1) is 4 |
length(A) |
In Octave: the element count for a vector, max(size(A)) for matrices and N-D arrays, 0 for empty |
4 |
ndims(A) |
Number of dimensions (Octave: never fewer than 2) | 3 |
The practical difference: for a vector, numel and length agree; for a matrix they do not. A 3-by-5 matrix has numel 15 but max(size) 5. Check that you want the total count before swapping one for the other.
Troubleshooting
numel of text returns the wrong number
Character vectors and string scalars count differently. Per the reference page, numel('mytext') is 6 because a char vector counts characters, while numel("mytext") is 1 because a string scalar is a single element.
numel of a table is smaller than expected
A table counts rows times variables, and a variable with several columns counts once. In the documented example the 5-by-4 table returns 20 even though one variable has two columns.
numel of a cell array counts cells, not their contents
The four-cell example returns 4 regardless of what is inside each cell.
Counting only part of an array
In Octave, numel(A, idx1, idx2, ...) returns the number of elements A(idx1, idx2, ...) would produce, and : is allowed. This is documented for Octave; it is not covered by the MATLAB example set above.
Free alternatives
| Tool | Equivalent | Notes (from its documentation) |
|---|---|---|
| GNU Octave | numel (A) |
Also accepts index arguments, numel (A, idx1, idx2, ...). Companion functions: length, size, ndims. Source: Octave manual, Object Sizes, read 2026-11-08. |
Related MATLAB guides
- MATLAB reshape: the element count must be preserved, which is what
numelchecks. - repmat in MATLAB: how output size is built from input size.
- Matrix in MATLAB: creating and transforming matrices.
- MATLAB sum: a related reduction function.
- MATLAB meshgrid: grids whose sizes you can check with
sizeandnumel.
FAQ
What does numel do in MATLAB?
It returns the number of elements in an array, equal to prod(size(A)) (MathWorks numel reference, read 2026-11-08).
What is the difference between numel and length?
numel counts all elements. In the Octave manual, length returns max(size(A)) for matrices and N-D arrays, so the two differ for anything but a vector.
How do I use numel online?
The call is the same in any MATLAB or Octave session: numel(A). Use the environment your institution provides.
Does numel count rows or columns?
Neither alone. It counts every element; use size(A,1) for rows and size(A,2) for columns.
Sources
- MathWorks, numel – Number of array elements, read 2026-11-08.
- GNU Octave documentation, Object Sizes, read 2026-11-08.







