How magic square, magic() FUNCTION works for Negative Integers & 2(where it should not work) ?

1 view (last 30 days)
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 Comment
the cyclist
the cyclist on 15 Mar 2013
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.

Sign in to comment.

Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!