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!