mycrosstab

Version 1.0.0.0 (2.42 KB) by Lee Baker
Cross-tabulation of vectors with replacement for missing levels.
327 Downloads
Updated 3 Aug 2010

View License

Takes 2 vectors of equal size and determines their cross-tabulation.
All input data must be real, positive integers.

Problem of built-in 'crosstab' function is that it does not take missing data into account. For example, given the data:

x y
1 0
1 1
1 1
2 0
2 1
4 0
4 0

This should give a contingency table of:

1 2
1 1
0 0 <- there is no data for x=3
2 0

However, the built-in function 'crosstab' would give the answer:

1 2
1 1
2 0

The function 'mycrosstab' uses 'crosstab' and then determines if any rows or columns are missing. If so, indices are added to the rows and columns and additional rows/columns with the appropriate indices are appended:

-1 0 1
1 1 2
2 1 1
4 2 0
3 0 0 <- additional column with index '3'

The rows and columns are then sorted so the indices are correctly ordered:

-1 0 1
1 1 2
2 1 1
3 0 0 <- additional column with index '3' now in appropriate place
4 2 0

The index row and column are then deleted, leaving a full cross-tabulation with all null rows and columns in the appropriate places:

1 2
1 1
0 0
2 0

Cite As

Lee Baker (2026). mycrosstab (https://www.mathworks.com/matlabcentral/fileexchange/28366-mycrosstab), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Version Published Release Notes
1.0.0.0