Solve Magic Square

Find all combinations of entries in a matrix which sum equals a certain number up to eps precision.


Updated 4 Apr 2013

View License

Find all combinations of entries in a matrix (or vector) which sum equals a certain number. In addition to the matrix, the length of the combination (two entries, three entries, ...) as well as the numerical value to match can be supplied. Works as well on vectors.

Returns the combination and the indices of the combinations.

Since it uses brute force to try all possible combinations the time strongly depends on the size of the matrix and the length of the combination.

Cite As

Bastian Tietjen (2023). Solve Magic Square (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Added floating point precision, progress should not decrease due to the polynom behind it