solving is magic function

How to write a function that will take a matrix m as an input and return true only if M is a magic square

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 17 日

0 投票

Create a magic matrix with magic function then use isequal to compare your two matrices

2 件のコメント

Walter Roberson
Walter Roberson 2013 年 10 月 17 日
編集済み: Walter Roberson 2013 年 10 月 17 日
Note: as magic() generates randomly, you will need to generate all of the magic squares of that size and compare each of them to the original matrix.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 18 日
Ok, I ignore it

サインインしてコメントする。

Walter Roberson
Walter Roberson 2013 年 10 月 17 日

0 投票

One test: calculate the mean of the numbers in the array. If it is not an integer, the array cannot be a magic square.
Another test: an array which is square and has all elements identical is a magic square.
A third test: magic squares are always two dimensional, not vectors and not three or more dimensions.

1 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 18 日
編集済み: Azzi Abdelmalek 2013 年 10 月 18 日
sum(A,1) ans sum(A,2) are equal and constant

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2013 年 10 月 17 日

コメント済み:

2013 年 10 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by