Why Magic Square[ magic(n) ] gives results diifferent on different situation?

8 vues (au cours des 30 derniers jours)
M.K.H. CHANDAN
M.K.H. CHANDAN le 26 Juin 2013
As per Magic Square Help/Defn.:
MAGIC 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.
Now in MATLAB ,
1. The output of magic(2) not valid for 2 sums. diagonal sums and row sums in MATLAB Version 7.5.0.342 (R2007b)
>> magic(2)
ans =
1 3
4 2
2.It should not work for negative nos , but showing different results for different negative inputs.
>> magic(-4) ??? Error using ==> reshape Size vector elements should be nonnegative.
Error in ==> magic at 26 M = reshape(1:n*n,n,n)';
>> magic(-5)
ans =
[]
>>
Manoj Harichandan>>> In above cases both -4 & -5 are both negative no's , but getting differnt results for different values, for all odd negative numbers it is showing null matrix and for all even negative numbers it is showing as : "Size vector elements should be nonnegative". It should show same error logs for both the cases. I think , From the start only it should stop the procedures to call other functions, as input given to the magic() function is wrong , instead of it is calling reshape function,the error is also showing , which are related to the error of reshape fuction.

Réponses (1)

Lokesh Ravindranathan
Lokesh Ravindranathan le 26 Juin 2013
Modifié(e) : Lokesh Ravindranathan le 26 Juin 2013
If you look at the documentation for magic function http://www.mathworks.com/help/matlab/ref/magic.html. There is an algorithms section which explains three different methods of implementation depending on the type of numbers. In your case, -4 is being treated as a number divisible by 4 and -5 is being treated as an odd number.
This is the reason for the difference in error messages.
  1 commentaire
M.K.H. CHANDAN
M.K.H. CHANDAN le 18 Mai 2015
Modifié(e) : M.K.H. CHANDAN le 18 Mai 2015
Yes,I know it ,
On newer version , it is updated only on help
but Can't we restrict it when argument is a negative after a function call, just on after entry to the function the function, instead of processing so many lines of code in MATLAB?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by