numel - Number of array elements - MATLAB (original) (raw)

Syntax

Description

n = numel([A](#btl24wx-1-A)) returns the number of elements, n, in array A, equivalent to prod(size(A)).

example

Examples

collapse all

Number of Elements in 3-D Matrix

Create a 4-by-4-by-2 matrix.

A = magic(4); A(:,:,2) = A'

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 counts 32 elements in the matrix.

String Array

Create a string array and compute the number of elements in the array.

A = ["a" "b" "c"; "d" "e" "f"]

A = 2x3 string "a" "b" "c" "d" "e" "f"

Number of Elements in Cell Array

Create a cell array of character vectors.

A = {'dog','cat','fish','horse'};

numel counts 4 elements in the array.

Number of Elements in Table

Create a table with four variables listing patient information for five people.

LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'}; Age = [38;43;38;40;49]; Height = [71;69;64;67;64]; Weight = [176;163;131;133;119]; BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];

A = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)

A=5×4 table Age Height Weight BloodPressure ___ ______ ______ _____________

Smith       38       71       176       124     93  
Johnson     43       69       163       109     77  
Williams    38       64       131       125     83  
Jones       40       67       133       117     75  
Brown       49       64       119       122     80  

Find the number of elements in the table.

numel returns a value equivalent to prod(size(A)) corresponding to the 5 rows and 4 variables.

Input Arguments

collapse all

A — Input array

scalar | vector | matrix | multidimensional array | table | timetable

Input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.

Tips

Extended Capabilities

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

Thenumel function fully supports tall arrays. For more information, see Tall Arrays.

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

The numel function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a