Documentation Center

  • Trial Software
  • Product Updates

levelcounts

Class: categorical

Element counts by level

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.

Syntax

C = levelcounts(A)
C = levelcounts(A,dim)

Description

C = levelcounts(A) for a categorical vector A counts the number of elements in A equal to each of the possible levels in A. The output is a vector C containing those counts, and has as many elements as A has levels. For matrix A, C is a matrix of column counts. For N-dimensional arrays, levelcounts operates along the first nonsingleton dimension.

C = levelcounts(A,dim) operates along the dimension dim.

Examples

Count the number of patients in each age group in the data in hospital.mat:

load hospital
edges = 0:10:100;
labels = strcat(num2str((0:10:90)','%d'),{'s'});
disp(labels')
 '0s' '10s' '20s' '30s' '40s' '50s' '60s' '70s' '80s' '90s'

AgeGroup = ordinal(hospital.Age,labels,[],edges);
I = islevel(labels,AgeGroup);
disp(I')
 0  1  1  1  1  1  1  1  1  1
c = levelcounts(AgeGroup);
disp(c')
 0  0 15 41 42  2  0  0  0  0

AgeGroup = droplevels(AgeGroup);
I = islevel(labels,AgeGroup);
disp(I')
 0  0  1  1  1  1  0  0  0  0
c = levelcounts(AgeGroup);
disp(c')
 15 41 42  2

See Also

|

Was this topic helpful?