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
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by