How magic square, magic() FUNCTION works for Negative Integers & 2(where it should not work) ?
1 回表示 (過去 30 日間)
古いコメントを表示
In Matlab(7.5): For -ve Odd order it is dispalying NULL matrix and for -ve Even order it is showing error.
??? Subscript indices must either be real positive integers or logicals.
For all other N > 0 & N !=2
Magic square.
MAGIC(N) is an N-by-N matrix constructed from the integers
1 through N^2 with equal row, column, and diagonal sums.
Produces valid magic squares for all N > 0 except N = 2.
works perfectly
Can't we restrict the process of calling function from the satrt itself ?
as ??? Subscript indices must either be real positive integers or logicals and should not equal to 2.
For Example >> >> magic(-2) ??? Subscript indices must either be real positive integers or logicals.
Error in ==> magic at 41 M([i; i+p],j) = M([i+p; i],j);
>> magic(-3)
ans =
[]
MK harichandan >> Here Null Matrix instead of error.
>> magic(-4) ??? Error using ==> reshape Size vector elements should be nonnegative.
Error in ==> magic at 26 M = reshape(1:n*n,n,n)';
MK harichandan >> Here error after processing some methods.
>> magic(-1)
ans =
[]
>> magic(-12) ??? Error using ==> reshape Size vector elements should be nonnegative.
Error in ==> magic at 26 M = reshape(1:n*n,n,n)';
MK harichandan>> Here error after processing some methods.
1 件のコメント
the cyclist
2013 年 3 月 15 日
I agree that the error-trapping of the magic() function leaves something to be desired. The fact that it returns a result for n=2, without warning or error, is particularly annoying.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!