Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

numel - Number of elements in array or subscripted array expression

Syntax

n = numel(A)
n = numel(A, index1, index2, ... indexn)

Description

n = numel(A) returns the number of elements, n, in array A.

n = numel(A, index1, index2, ... indexn) returns the number of subscripted elements, n, in A(index1, index2, ..., indexn). To handle the variable number of arguments, numel is typically written with the header function n = numel(A, varargin), where varargin is a cell array with elements index1, index2, ... indexn.

The MATLAB software implicitly calls the numel built-in function whenever an expression generates a comma-separated list. This includes brace indexing (i.e., A{index1,index2,...,indexN}), and dot indexing (i.e., A.fieldname).

Remarks

It is important to note the significance of numel with regards to the overloaded subsref and subsasgn functions. In the case of the overloaded subsref function for brace and dot indexing (as described in the last paragraph), numel is used to compute the number of expected outputs (nargout) returned from subsref. For the overloaded subsasgn function, numel is used to compute the number of expected inputs (nargin) to be assigned using subsasgn. The nargin value for the overloaded subsasgn function is the value returned by numel plus 2 (one for the variable being assigned to, and one for the structure array of subscripts).

As a class designer, you must ensure that the value of n returned by the built-in numel function is consistent with the class design for that object. If n is different from either the nargout for the overloaded subsref function or the nargin for the overloaded subsasgn function, then you need to overload numel to return a value of n that is consistent with the class' subsref and subsasgn functions. Otherwise, MATLAB produces errors when calling these functions.

Examples

Create a 4-by-4-by-2 matrix. numel counts 32 elements 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

See Also

nargin, nargout, prod, size, subsasgn, subsref

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS