## Documentation Center |

Cross-tabulation

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

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

Was this topic helpful?