count

Returns the number of elements in the argument.

Syntax

n = count ( variable ) ;

Arguments

list variable

Return Value

int n

Exceptions

%ARGUMENT : Invalid arguments. Usage: n = count ( variable ) ;

Description

The count function returns the number of elements in a list or vector. From a dynamic list, which is the default variable type, the count function must hop through each element in the list to get the current count, so it is inefficient for large lists. For example, the second form of the for statement below is more efficient:

/* List of 1000+ elements */
list a = { 1,2,3,4,.......,1001,1002,...} ;
/* count(a) is done each time through the loop!! */
for (i=0;i<count(a);i++) { .... }
/* count(a) is done upfront, loop is now faster */
n = count a ; for (i=0;i<n;i++) { ... }

Examples

To execute the example below, press or modify the example to try different variations.

Related Links

exists