MATLAB Function Reference | ![]() ![]() |
Number of elements in a matrix
Syntax
n = numel(a)
Description
n = numel(a)
returns the scalar count, n
, of the number of elements in the matrix, a
.
numel(a)
gives the same answer as prod(size(a))
. However, if the size
function has been overloaded, prod(size(a))
may not provide an accurate count.
numel
can also be used with subsref
to determine the number of values that will be returned from a particular call to subsref
. See the second example below to see how to use this.
Examples
Create a 4-by-4-by-2 matrix. numel
counts 32 elments in the matrix.
a = magic(4); a(:,:,2) = a' a(:,:,1) = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 a(:,:,2) = 16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1 numel(a) ans = 32
In this example, numel
indicates that stockobj(3)
references six values. The indexed reference to stockobj
is made using subsref
.
n = numel(stockobj(3)) n = 6
Calling subsref
on stockobj(3)
does indeed return six values.
stockobj(3) ans = 1.0417 5.2000 7.0000 39.0400 4.2200 56.4340
See Also
![]() | num2str | nzmax | ![]() |