numel — Count the number of elements in scalars, vectors, matrices, and N-D arrays.
numel(A) returns the number of elements contained in A, matching MATLAB semantics for dense arrays, logical arrays, cell arrays, structs, strings, and character arrays. The result is always a double-precision scalar.
How numel works in RunMat
numel(A)counts every stored element; for matrices this isprod(size(A)).numel(A, dim1, dim2, ...)multiplies the extents of the requested dimensions. Each dimension argument must be a positive integer. Dimensions beyond the array rank contribute a factor of1.- Passing a numeric vector of dimensions (for example
numel(A, [1 3])) is equivalent to listing them separately. - Scalars return
1, character arrays reportrows × cols, and cell arrays count the number of cells. - Empty arrays return
0when any dimension is zero. - GPU-resident arrays use metadata stored on the device handle; when unavailable, RunMat gathers the data to maintain correctness.
How numel runs on the GPU
numel does not launch GPU kernels or register provider hooks. When the input is a GPU tensor, the runtime consults the shape metadata stored in the handle to compute the count. If the active provider omits this metadata, RunMat downloads the tensor once to recover the correct answer. The builtin always returns a host double scalar and never allocates device memory, so it can safely be used inside fused GPU expressions without breaking residency.
Examples
Counting elements in a matrix
A = [1 2 3; 4 5 6];
n = numel(A)Expected output:
n = 6Checking the number of elements in a cell array
C = {1, 2, 3; 4, 5, 6};
cells = numel(C)Expected output:
cells = 6Getting the number of elements along selected dimensions
T = rand(4, 3, 2);
plane = numel(T, 1, 2); % product of the first two dimensionsExpected output:
plane = 12Using numel with gpuArray data
G = gpuArray(ones(256, 4));
count = numel(G)Expected output:
count = 1024Confirming the number of characters in a char array
name = ['R' 'u' 'n' 'M' 'a' 't'];
chars = numel(name)Expected output:
chars = 6FAQ
How is numel different from length?
numel(A) counts every element in A, whereas length(A) returns the size of the largest single dimension.
What happens when I specify dimensions?
numel(A, dim1, dim2, ...) multiplies the lengths of the listed dimensions. Missing dimensions (for example, requesting the fourth dimension of a matrix) contribute a factor of 1, matching MATLAB.
Does numel gather GPU data?
Usually no. The runtime reads shape metadata from the GPU tensor handle. It gathers only when the provider fails to populate that metadata.
What does numel return for empty arrays?
If any dimension is zero, numel returns 0. This behaviour matches MATLAB for empty matrices, cell arrays, and logical arrays.
Can I use numel on strings and character arrays?
Yes. Character arrays return the total number of characters (rows × cols). String arrays are treated as ordinary arrays with MATLAB-compatible dimensions.
Is numel safe inside fused GPU expressions?
Yes. The builtin returns a host double scalar and does not allocate device buffers, so fusion plans remain intact.
Does numel accept non-integer dimensions?
No. Dimension arguments must be positive integers. Fractional, negative, or non-numeric values raise an error that mirrors MATLAB.
What does numel do in MATLAB?
numel(A) returns the total number of elements in array A. It is equivalent to prod(size(A)). In RunMat, numel works identically and supports GPU arrays.
Is numel the same as length in MATLAB?
No. numel returns the total element count across all dimensions, while length returns the size of the largest dimension. For a 3×4 matrix, numel returns 12 but length returns 4.
Can I use numel with cell arrays and structs?
Yes. numel counts the number of cells in a cell array or the number of elements in a struct array. It does not recurse into cell contents or struct fields.
Related functions to explore
These functions work well alongside numel. Each page has runnable examples you can try in the browser.
size, length, numel, size, isempty, ismatrix, isscalar, isvector, ndims
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how numel works, line by line, in Rust.
- View numel.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.