Main Content

levelcounts

(Not Recommended) Element counts by level of a nominal or ordinal array

The nominal and ordinal array data types are not recommended. To represent ordered and unordered discrete, nonnumeric data, use the Categorical Arrays data type instead.

Description

C = levelcounts(A) returns counts of the number of elements in the nominal or ordinal array A equal to each possible level in A.

  • If A is a vector, then C is a vector containing as many elements as the number of levels in A.

  • If A is a matrix, then C is a matrix of column counts.

  • If A is an N-dimensional array, then levelcounts operates along the first nonsingleton dimension.

example

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

Examples

collapse all

Create a nominal array from data in a cell array.

colors = nominal({'r','b','g';'g','r','b';'b','r','g'},...
                 {'blue','green','red'})
colors = 3x3 nominal
     red        blue      green 
     green      red       blue  
     blue       red       green 

Count the number of observations of each level in each column.

levelcounts(colors)
ans = 3×3

     1     1     1
     1     0     2
     1     2     0

Count the number of observations of each level in each row.

levelcounts(colors,2)
ans = 3×3

     1     1     1
     1     1     1
     1     1     1

Alternatively, you can use summary to display the counts with their labels. The default is to count elements in each column.

summary(colors)
     blue       1      1      1 
     green      1      0      2 
     red        1      2      0 

You can also count elements in each row.

summary(colors,2)
     blue      green      red 
     1         1          1   
     1         1          1   
     1         1          1   

Input Arguments

collapse all

Nominal or ordinal array, specified as a nominal or ordinal array object created with nominal or ordinal.

Dimension along which to count the number of elements in each level, specified as a positive integer value. For example, if the dimension is 1, then levelcounts counts along each column, while if the dimension is 2, then levelcounts counts along each row.

Data Types: double | single

See Also

| |

Introduced in R2007a