View all functions

CategoryArray: Introspection
GPUYes

What does the numel function do in MATLAB / RunMat?

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 does the numel function behave in MATLAB / RunMat?

  • numel(A) counts every stored element; for matrices this is prod(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 of 1.
  • Passing a numeric vector of dimensions (for example numel(A, [1 3])) is equivalent to listing them separately.
  • Scalars return 1, character arrays report rows × cols, and cell arrays count the number of cells.
  • Empty arrays return 0 when any dimension is zero.
  • GPU-resident arrays use metadata stored on the device handle; when unavailable, RunMat gathers the data to maintain correctness.

numel Function GPU Execution Behaviour

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 of using the numel function in MATLAB / RunMat

Counting elements in a matrix

A = [1 2 3; 4 5 6];
n = numel(A);

Expected output:

n = 6;

Checking the number of elements in a cell array

C = {1, 2, 3; 4, 5, 6};
cells = numel(C);

Expected output:

cells = 6;

Getting the number of elements along selected dimensions

T = rand(4, 3, 2);
plane = numel(T, 1, 2);   % product of the first two dimensions

Expected output:

plane = 12;

Using numel with gpuArray data

G = gpuArray(ones(256, 4));
count = numel(G);

Expected output:

count = 1024;

Confirming the number of characters in a char array

name = ['R' 'u' 'n' 'M' 'a' 't'];
chars = numel(name);

Expected output:

chars = 6;

FAQ

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.

See Also

size, length, MathWorks numel reference, MathWorks size reference