Products & Services Solutions Academia Support User Community Company

Learn more about Statistics Toolbox   

crosstab - Cross-tabulation

Syntax

table = crosstab(x1,x2)
table = crosstab(x1,x2,x3,...)
[table,chi2,p] = crosstab(...)
[table,chi2,p,labels] = crosstab(...)

Description

table = crosstab(x1,x2) returns a cross-tabulation table of two vectors of the same length x1 and x2. table is m-by-n, where m is the number of distinct values in x1 and n is the number of distinct values in x2.

x1 and x2 are grouping variables, as described in Grouped Data. crosstab uses grp2idx to assign a positive integer to each distinct value. table(i,j) is a count of indices where grp2idx(x1) is i and grp2idx(x2) is j. The numerical order of grp2idx(x1) and grp2idx(x2) order rows and columns of table, respectively.

table = crosstab(x1,x2,x3,...) returns a multi-dimensional table where table(i,j,k,...) is a count of indices where grp2idx(x1) is i, grp2idx(x2) is j, grp2idx(x3) is k, and so on.

[table,chi2,p] = crosstab(...) also returns the chi-square statistic chi2 and its p-value p for a test that table is independent in each dimension. The null hypothesis is that the proportion in any entry of table is the product of the proportions in each dimension.

[table,chi2,p,labels] = crosstab(...) also returns a cell array labels with one column for each input argument. The entries in the first column are labels for the rows of table, the entries in the second column are labels for the columns, and so on, for a multi-dimensional table.

Examples

Example 1

Cross-tabulate two vectors with three and four distinct values, respectively:

x = [1 1 2 3 1]; y = [1 2 5 3 1];
table = crosstab(x,y)
table =
     2     1     0     0
     0     0     0     1
     0     0     1     0

Example 2

Generate two independent vectors, each containing 50 discrete uniform random numbers in the range 1:3:

x1 = unidrnd(3,50,1);
x2 = unidrnd(3,50,1);
[table,chi2,p] = crosstab(x1,x2)
table =
     1     6     7
     5     5     2
    11     7     6
chi2 =
    7.5449
p =
    0.1097

At the 95% confidence level, the p-value fails to reject the null hypothesis that table is independent in each dimension.

Example 3

The file carbig.mat contains measurements of large model cars during the years 1970-1982:

load carbig
[table,chi2,p,labels] = crosstab(cyl4,when,org)
table(:,:,1) =
    82    75    25
    12    22    38
table(:,:,2) =
     0     4     3
    23    26    17
table(:,:,3) =
     3     3     4
    12    25    32

chi2 =
  207.7689

p =
     0

label = 
    'Other'    'Early'    'USA'   
    'Four'     'Mid'      'Europe'
         []    'Late'     'Japan' 

table and label together show that the number of four-cylinder cars made in the USA during the late period of the data was table(2,3,1) or 38 cars.

See Also

Grouped Data

tabulate, grp2idx

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS