Quantcast

Documentation Center

  • Trial Software
  • Product Updates

crosstab

Cross-tabulation

Syntax

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

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 Grouping Variables. 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,...,xn) returns a multi-dimensional table where table(i,j,...,n) 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(x1,...,xn) 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(x1,...,xn) 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

expand all

Cross-Tabulate Two Data Vectors

This example shows how to cross-tabulate two data vectors.

Create two sample data vectors, containing three and four distinct values, respectively.

x = [1 1 2 3 1];
y = [1 2 5 3 1];

Cross-tabulate x and y.

table = crosstab(x,y)
table =

     2     1     0     0
     0     0     0     1
     0     0     1     0

Cross-Tabulate Independent Data Vectors

This example shows how to cross-tabulate two independent data vectors.

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

rng('default');  % For reproducibility
x1 = unidrnd(3,50,1);
x2 = unidrnd(3,50,1);

Cross-tabulate x1 and x2.

[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.

Cross-Tabulate Grouped Data

This example shows how to cross-tabulate grouped sample data.

Load the sample data, which contains measurements of large model cars during the years 1970-1982.

load carbig

Cross-tabulate the data of four-cylinder cars (cyl4) based on model year (when) and country of origin (org).

[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 =

   8.0973e-38


labels = 

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

The results in table and labels together show that the number of four-cylinder cars made in the USA during the late period of the data is located in table(2,3,1), and equals 38 cars.

See Also

|

Was this topic helpful?