Categories &

Functions List

Function Reference: tabulate

statistics: tabulate (x)
statistics: tbl = tabulate (x)

Create a frequency table of unique values in vector x.

tabulate (x) displays a frequency table of the data in the vector x. The input x can be a numeric vector, a logical vector, a character array, a cell array of strings, a categorical array, or a string array.

The table displays the value, the number of instances (count), and the percentage of that value in x. If no output argument is requested, the table is displayed in the command window.

tbl = tabulate (x) returns the frequency table, tbl, as a numeric matrix when x is numeric and as a cell array otherwise.

If x is numeric, any missing values (NaNs) are ignored. Similarly, undefined elements in categorical arrays and missing elements in string arrays are ignored.

If all the elements of x are positive integers, then the frequency table includes 0 counts for the integers between 1 and max (x) that do not appear in x.

For categorical arrays, the frequency table includes 0 counts for any categories that are defined but do not appear in x.

See also: bar, pareto

Source Code: tabulate

Example: 1

 

 ## Generate a frequency table for a vector of data in a cell array
 load patients

 ## Display the first seven entries of the Gender variable
 gender = Gender(1:7)

 ## Compute the frequency table that shows the number and
 ## percentage of Male and Female patients
 tabulate (Gender)

gender =
  7x1 cell array

    {'Male'  }    
    {'Male'  }    
    {'Female'}    
    {'Female'}    
    {'Female'}    
    {'Female'}    
    {'Female'}    

  2x3 table

      Value       Count    Percent    
    __________    _____    _______    

    {'Male'  }       47         47    
    {'Female'}       53         53    

                    

Example: 2

 

 ## Create a frequency table for a vector of positive integers
 load patients

 ## Display the first seven entries of the Gender variable
 height = Height(1:7)

 ## Create a frequency table that shows, in its second and third columns,
 ## the number and percentage of patients with a particular height.
 table = tabulate (Height);

 ## Display the first and last seven entries of the frequency table
 first = table(1:7,:)

 last = table(end-6:end,:)

height =

   71
   69
   64
   67
   64
   68
   64

first =

   1   0   0
   2   0   0
   3   0   0
   4   0   0
   5   0   0
   6   0   0
   7   0   0

last =

   66   15   15
   67    6    6
   68   15   15
   69    8    8
   70   11   11
   71   10   10
   72    4    4

                    

Example: 3

 

 ## Create a frequency table from a character array
 load carsmall

 ## Tabulate the data in the Origin variable, which shows the
 ## country of origin of each car in the data set
 tabulate (Origin)

  6x3 table

       Value       Count    Percent    
    ___________    _____    _______    

    {'USA'    }       69         69    
    {'France' }        4          4    
    {'Japan'  }       15         15    
    {'Germany'}        9          9    
    {'Sweden' }        2          2    
    {'Italy'  }        1          1    

                    

Example: 4

 

 ## Create a frequency table from a numeric vector with NaN values
 load carsmall

 ## The carsmall dataset contains measurements of 100 cars
 total_cars = length (MPG)
 ## For six cars, the MPG value is missing
 missingMPG = length (MPG(isnan (MPG)))

 ## Create a frequency table using MPG
 tabulate (MPG)
 table = tabulate (MPG);

 ## Only 94 cars were used
 valid_cars = sum (table(:,2))

total_cars = 100
missingMPG = 6
  37x3 table

    Value    Count    Percent    
    _____    _____    _______    

        9        1    1.06383    
       10        2    2.12766    
       11        1    1.06383    
       13        4    4.25532    
       14        5    5.31915    
     14.5        1    1.06383    
       15        5    5.31915    
     15.5        1    1.06383    
       16        2    2.12766    
     16.5        2    2.12766    
       17        1    1.06383    
     17.5        2    2.12766    
       18        4    4.25532    
     18.5        1    1.06383    
       19        2    2.12766    
       20        2    2.12766    
       21        2    2.12766    
       22        4    4.25532    
     22.5        1    1.06383    
       23        1    1.06383    
       24        4    4.25532    
     24.5        1    1.06383    
       25        5    5.31915    
       26        4    4.25532    
     26.5        1    1.06383    
       27        6    6.38298    
       28        4    4.25532    
       29        3    3.19149    
     29.5        1    1.06383    
       31        3    3.19149    
       32        4    4.25532    
       33        1    1.06383    
       34        2    2.12766    
       36        5    5.31915    
       37        1    1.06383    
       38        4    4.25532    
       44        1    1.06383    

valid_cars = 94