numel — Return the number of outer array elements.
numel computes an exact checked product of visible dimensions and includes a compatibility-gated RunMat extension for selected dimensions.
Syntax
n = numel(A)
n = numel(A, dim1, dim2, ...)Inputs
A- Type
Any- Required
- Yes
- Description
- Value whose outer array elements are counted.
dim- Type
SizeArg- Required
- Variadic
- Description
- RunMat-only positive integer dimension selectors.
Returns
n- Type
NumericScalar- Description
- Exact element count as a host double scalar.
Errors
RunMat:numel:InvalidDimensionType- When
- A selector is not a numeric scalar or vector.
- Message
numel: dimension selectors must be numeric scalars or one numeric vector
RunMat:numel:InvalidDimensionVector- When
- The single vector selector is not vector-shaped.
- Message
numel: dimension selector must be a vector
RunMat:numel:InvalidDimensionList- When
- A vector occurs within a variadic scalar selector list.
- Message
numel: separate dimension arguments must be scalars
Element count
numel(A) returns the product of the MATLAB-visible dimensions of A as a host double scalar. It counts outer array elements: cells rather than their contents, table rows times variables, string elements rather than characters within each string, and every character in a character array.
Any zero extent makes the result zero. Checked structural arithmetic detects an overflow before a result is formed, and RunMat rejects a count that the documented double result cannot represent exactly.
Selected-dimension extension
In RunMat compatibility mode, numel(A, dim1, dim2, ...) returns the checked product of selected extents. One dimension vector is also accepted. All eight integer classes are decoded exactly, dimensions beyond the visible rank contribute 1, and an empty selector is rejected.
The selected-dimension forms are a named RunMat extension. A MATLAB compatibility policy accepts only numel(A), matching the public compatible syntax.
Resident and distributed values
gpuArray and distributed inputs are counted from validated shape metadata. numel launches no kernel, calls no acceleration provider, transfers no payload, and returns one host scalar at a fusion boundary.
Examples
Count matrix elements
A = [1 2 3; 4 5 6];
n = numel(A)Expected output:
n = 6Count an N-D array
A = zeros(4, 4, 2);
n = numel(A)Expected output:
n = 32Count cells without inspecting their contents
C = {1, 2, 3; 4, 5, 6};
n = numel(C)Expected output:
n = 6Count characters in a character row
name = 'RunMat';
n = numel(name)Expected output:
n = 6Count an empty array
A = zeros(0, 7);
n = numel(A)Expected output:
n = 0Multiply selected dimensions
A = zeros(4, 3, 2);
n = numel(A, 1, 2)Expected output:
n = 12Count a resident array without gathering
G = gpuArray(ones(256, 4));
n = numel(G)Expected output:
n = 1024Using numel with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how numel changes the result.
Run a small numel example, explain the result, then change one input and compare the output.
FAQ
How is numel different from length?⌄
numel multiplies every dimension; length returns only the largest dimension.
When should I use size instead?⌄
Use size when individual extents or the complete shape are needed.
What does numel count in a cell array or struct array?⌄
It counts outer cells or struct elements and does not recurse into their contents or fields.
How are strings and character arrays different?⌄
A string scalar is one string-array element. A character row has one element per character.
What happens for an empty array?⌄
The product is zero when any visible extent is zero.
Are selected dimensions compatible syntax?⌄
No. They are enabled only by RunMat compatibility mode; the compatible public form is numel(A).
Does numel gather gpuArray or distributed data?⌄
No. Complete shape metadata is part of each validated handle.
Related Array functions
Introspection
height · iscolumn · isempty · ismatrix · isrow · isscalar · isvector · length · ndims · size · width
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how numel is executed, line by line, in Rust.
- View the source for numel in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.