Maths & numbers

Integer matrix contingency table count calculator from margins

Fixed row and column sums can admit many different nonnegative integer matrices.

Your numbers

One to five nonnegative margins, each 0–30; total ≤40.
One to five margins with the same grand total as the row sums.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Fixed row and column sums can admit many different nonnegative integer matrices. This calculator counts those labeled tables exactly by allocating one row at a time and remembering the remaining column margins. A separate example enumeration shows at most twenty tables without confusing that display cap with the complete count. Equal-valued rows and columns remain labeled positions rather than being identified under permutations.

THE FORMULACount A∈Z_(≥0)^(r×c) with Σ_j a_ij=R_i and Σ_i a_ij=C_j, using row-allocation dynamic programming.

A worked example

With row sums (2,2) and column sums (2,2), every table has form [[a,2−a],[2−a,a]], where a is zero, one or two. There are exactly three tables. The row and column labels matter: swapping rows can produce another table already counted in the sample space, not a symmetry to divide out. The example display contains all three because the total is below twenty.

Good to know

Use one to five row margins and one to five column margins, entries from zero through thirty, equal grand totals and a grand total no greater than forty. Cells are nonnegative integers with no structural zeros or additional bounds. A 500,000-state search budget rejects excessive requests. The example list is capped at twenty; the main count is exact and complete for accepted requests.

A feasible cell range follows from the remaining margins

When allocating a row with remaining total T, a candidate cell cannot exceed its column’s unused margin. It also cannot be so small that the later columns lack enough remaining capacity to absorb the rest of T. These lower and upper bounds enumerate all possible row vectors without generating rows that already violate a margin.

After a row vector is chosen, subtract it from the remaining column margins and recurse to the next labeled row. States with the same row index and remaining column vector have the same number of completions, so memoization avoids recomputing them. The method counts integer allocations, not real-valued points in a transportation polytope.

Three two-by-two tables share row and column sums two comma two0220a = 01111a = 12002a = 2
All solutions are [[a,2−a],[2−a,a]] for a=0,1,2. Every row sum and column sum is two. Rows and columns retain their labels; no symmetry division is applied.

The Matrix balancing calculator with target row column totals scales a real-valued matrix to target margins, while this page counts distinct integer tables satisfying them.

Do not divide by row or column permutations

Row one and row two may represent different categories even when their marginal totals match. Their positions are therefore retained. Dividing a count by the factorial of the number of rows would incorrectly identify tables under an equivalence relation the input never requested, and symmetry stabilizers would make such a shortcut invalid even for an unlabeled problem.

The same applies to columns. The default two-by-two example has three labeled solutions, with one determined by each allowable upper-left value. A table-balancing algorithm may produce one real-valued matrix satisfying those margins, but that output neither enumerates nor counts the distinct integer tables. The two calculators answer different questions.

Zero margins and capped examples need separate interpretations

A zero row margin forces every cell in that row to zero; a zero column does the same vertically. With all margins zero there is exactly one table, the all-zero matrix. It is not an empty solution set. Unequal grand totals, by contrast, are incompatible inputs and are rejected before enumeration.

The result count can exceed twenty while the displayed examples remain capped. The count is obtained from the complete dynamic recursion, not estimated from those examples. If the total search budget is exceeded, the request fails instead of returning the examples seen so far as if they established a complete count. Small cases are checked by an independent cell-by-cell exhaustive enumerator.

A few questions, answered

Are equal-sum rows interchangeable?

No. Rows and columns are labeled by their input positions. Equal margins do not remove those labels or identify permuted tables.

Does twenty examples mean there are twenty solutions?

Not necessarily. Read the main exact count. The example list is a separate display capped at twenty tables.

Find your calculator

Start typing to explore the library.