Statistics Toolbox    
crosstab

Cross-tabulation of several vectors

Syntax

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 noninteger values, 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

We generate 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 we would be surprised if p were near zero.

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

Example 2

We 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?

See Also
tabulate


  cov daugment