How this calculation works
Matrix balancing adjusts the relative sizes of nonnegative seed cells while meeting specified row and column totals. This calculator first uses an exact rational transportation network to test whether the positive seed support can realize those margins. It identifies positive seed cells that the margins force to zero, then applies iterative proportional fitting to the remaining support. Each sweep rescales rows and columns in turn. The result reports convergence, the largest absolute and relative margin residuals and the fitted matrix; a capped unconverged iterate is explicitly labeled approximate.
Alternately scale each row and column to its target margin; retain structural zeros and test maximum relative margin residual ≤10⁻¹⁰.A worked example
Start with a 2×2 matrix of ones, target row totals 30 and 70, and target column totals 40 and 60. The balanced matrix is ((12,18),(28,42)), preserving total one hundred. Its rows sum to thirty and seventy, and its columns to forty and sixty. Uniform seed odds make this particular result the independent outer-product table. A different nonuniform seed generally retains a different association pattern, so matrix balancing is not automatically the same as replacing every input by independent expected frequencies.
Good to know
Supports nonnegative rational seed matrices up to 6×6 and matching nonnegative target margins with exactly equal grand totals. Input parser bounds apply. Structural zeros remain zero; exact residual-flow analysis also finds positive seed cells forced to zero by the margins. Numerical IPF allows up to 20,000 sweeps and requires maximum relative margin residual at most 10⁻¹⁰ for convergence. A limit result remains explicitly approximate. No negative cells, unknown margins, integer rounding, disclosure protection or inference about observed-table uncertainty is provided.
Reference: R cat package — IPF and structural zeros (opens in a new tab) ↗
Equal grand totals do not guarantee that the permitted cells can carry them
A seed zero is a structural restriction, not simply a small starting value. If only diagonal cells are positive, row and column targets must be compatible with that diagonal support. Equal grand totals alone cannot move mass to a forbidden off-diagonal cell. The preliminary transportation check routes every required margin through positive seed cells using exact rational capacities. An insufficient feasible flow returns an infeasible-support status before a misleading numerical fit is attempted.
Some margins also force an originally positive cell to zero. For seed ((1,1),(0,1)) with both row and column targets equal to one, the upper-right cell must become zero. Ordinary scaling can approach that boundary slowly. The calculator examines residual circulation support to identify these forced zeros exactly and removes them before the remaining numerical iteration. It does not discard an arbitrary zero-flow cell from one feasible routing, since another feasible routing might give that cell positive mass.
The fitted association comes from the seed, not only the margins
Multiplying complete rows and columns by positive factors preserves supported cross-product ratios. A uniform positive seed therefore produces the familiar independent table under two-way margins, but a nonuniform seed can preserve an existing association. The target margins determine totals while the seed supplies relative internal structure. Two users entering the same margins and different seeds need not obtain the same balanced cells.
Convergence is measured from achieved versus requested margins, not merely from a small change between successive matrices. The table includes achieved row totals and the steps list column totals for inspection. A maximum-iteration message does not certify balanced margins. The Chi square contingency independence calculator instead evaluates a statistical independence hypothesis from observed counts; balancing a seed to margins neither computes that test nor establishes that the fitted table describes an independent real-world process. Exact integer allocation after balancing is also a separate problem.
A few questions, answered
Can a zero seed cell become positive?
No. Seed zeros are structural zeros in this model. If the requested margins need such a route, the support feasibility test can report infeasibility.
Does a converged table contain exact rational fitted cells?
Not in general. Feasibility and forced-zero checks are exact, but the IPF fit is numerical and is accepted only under the stated margin-residual tolerance.