crosstab - Cross-tabulation

Syntax

table = crosstab(col1,col2)
table = crosstab(col1,col2,col3,...)
[table,chi2,p] = crosstab(col1,col2)
[table,chi2,p,label] = crosstab(col1,col2)

Description

table = crosstab(col1,col2) takes two vectors of positive integers and returns a matrix, table, of cross-tabulations. The ijth element of table contains the count of all instances where col1 = i and col2 = j.

Alternatively, col1 and col2 can be vectors containing non-integer values, categorical variables, character arrays, or cell arrays of strings. crosstab implicitly assigns a positive integer group number to each distinct value in col1 and col2, and creates a cross-tabulation using those numbers.

table = crosstab(col1,col2,col3,...) returns table as an n-dimensional array, where n is the number of arguments you supply. The value of table(i,j,k,...) is the count of all instances where col1 = i, col2 = j, col3 = k, and so on.

[table,chi2,p] = crosstab(col1,col2) also returns the chi-square statistic, chi2, for testing the independence of the rows and columns of table. The scalar p is the significance level of the test. Values of p near zero cast doubt on the assumption of independence of the rows and columns of table.

[table,chi2,p,label] = crosstab(col1,col2) also returns a cell array label that has one column for each input argument. The value in label(i,j) is the value of colj that defines group i in the jth dimension.

Example

Example 1

This example generates 2 columns of 50 discrete uniform random numbers. The first column has numbers from 1 to 3. The second has only the numbers 1 and 2. The two columns are independent so it would be surprising if p were near zero.

r1 = unidrnd(3,50,1);
r2 = unidrnd(2,50,1);
[table,chi2,p] = crosstab(r1,r2)
table =

  10   5
   8   8
   6  13
chi2 =
  4.1723
p =
  0.1242

The result, 0.1242, is not a surprise. A very small value of p would make you suspect the "randomness" of the random number generator.

Example 2

Suppose you have data collected on several cars over a period of time. How many four-cylinder cars were made in the USA during the late part of this period?

[t,c,p,l] = crosstab(cyl4,when,org);
l
l = 
  'Other'  'Early'  'USA'  
  'Four'   'Mid'    'Europe'
    []     'Late'   'Japan' 
t(2,3,1)
ans =
  38

See Also

tabulate

  


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