Sparse Matrix Generator
A sparse matrix is one where storing the zeros costs more than storing the positions. Compressed sparse row keeps a value and a column index per non-zero entry, plus one pointer per row — so the crossover is around a third full, and above that the sparse form is genuinely worse. This generates matrices at any density and states which side of that line you are on rather than assuming sparse always wins.
What this generator does
Fills a matrix so that roughly the chosen share of cells are non-zero, then reports both storage costs: one number per cell for the dense form, versus a value and a column index per entry plus a row pointer each for compressed sparse row.
How to use this tool
- Choose the matrix shape and roughly what share of cells should be non-zero.
- Read the dense grid and the storage comparison.
- Push the density up past a third and watch sparse storage lose.
- Copy the dense form if you need test data.
Understanding the controls
- Rows
- Height of the matrix, up to 16 — beyond that the dense grid stops being readable.
- Columns
- Width of the matrix, up to 16. Non-square shapes are fine and often more realistic.
- Density (%)
- Roughly what share of cells are non-zero. Below about 33% sparse storage wins; above it, it does not.
- Seed
- Reproduces exactly the same matrix, which is what makes it usable as a benchmark fixture.
Common use cases
- Test data for sparse matrix code at a chosen density
- Showing where compressed storage stops paying off
- Teaching the compressed-sparse-row layout with real pointers
- Benchmark inputs at a controlled sparsity
- Reproducing the same matrix from a seed
How this generator works
Each cell is filled independently with the given probability, so the realised density varies slightly around what was asked for. The check reconstructs the dense matrix from the stored entries and requires exactly one non-zero per entry, rejects any stored zero or duplicated cell, and confirms the row pointers are non-decreasing and end at the entry count.
Randomness and fairness
Which cells are non-zero and what they hold is random; the density and storage figures are measured from the result. Seeded matrices reproduce exactly and are therefore explicitly not cryptographically secure, and unseeded ones use the browser's cryptographically secure generator.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- Non-zero positions are independent, so the block and banded structures real sparse matrices usually have do not appear.
- Values are single digits, chosen for readability rather than realism.
- Only compressed sparse row is shown; the column-major and coordinate formats are not.
- Matrices stop at sixteen by sixteen so the dense grid stays readable.
- Matrices are not stored between visits.
Privacy and your data
The matrix is generated in your browser. Nothing about its contents or your seed is transmitted or kept.
Related generators
- Random Matrix GeneratorA random 3x3 grid of numbers, for maths practice or placeholder data.
- Matrix Question GeneratorMultiplication, determinant and inverse questions, with inverses built to have whole-number entries.
- Hash Table Scenario GeneratorKeys distributed into buckets by modulo hashing, with collisions, chain lengths and empty buckets all recounted from the table.
- Binary Heap GeneratorMin or max heaps built one insertion at a time, shown level by level, and drained to prove they sort.