MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Check whether the input matrix is a normal magic square:

http://en.wikipedia.org/wiki/Magic_square

Output the logical value true or false.

205 correct solutions
965 incorrect solutions

Last solution submitted on Jun 26, 2015

4 players like this problem

2 Comments

Chellammai C
on 23 Jan 2015

x = magic(2);
y_correct = false;
This is wrong!!!! hence solution failed

the cyclist
on 27 Jan 2015

Although MATLAB does create a 2x2 matrix as an output from magic(2), that output is not a magic square.

1 player likes this solution

2 Comments

Dan K
on 14 Mar 2012

I'm pretty sure test 5 is wrong. magic(3)+1: sum of every row, column, and both diagonals is 18

Kevin Holst
on 2 May 2012

It's not a normal magic square if it's of size 3x3 and doesn't sum to 15. All rows/cols/diags must sum to n*(n^2+1)/2.

1 player likes this solution

1 Comment

David Young
on 22 Feb 2012

I don't think this checks the diagonals. Is that because it's not necessary?

2 Comments

Aurelien Queffurust
on 10 Feb 2012

issame is from which toolbox ?

Prateep Mukherjee
on 19 Mar 2012

($matlabroot)/toolbox/robust/rctobsolete/robust/issame.m

1 Comment

wenwu xiu
on 7 Feb 2012

passed in matlab but not here??what ??

2 Comments

Trung Duong
on 4 Feb 2012

Test 4 is wrong: y_correct should be = TRUE

David Young
on 22 Feb 2012

No, test 4 is correct. magic(2) does not return a magic square.

2 Comments

Aurelien Queffurust
on 10 Feb 2012

Hein? -348052801600 where does this number come from?!

David Young
on 22 Feb 2012

Note that this gives the wrong result for magic(4). I suspect that this is tailored to the test set.

1 Comment

Mark
on 8 Apr 2012

I like this since there is nothing in the MATLAB doc which guarantees that magic() produces a normal magic square. Is it necessary to also check both diagonals?

1 Comment

Peter Wittenberg
on 31 Mar 2012

Folks, I don't get it. If you create z= magic(3)', that's a magic square. No surprise. If you then do an isequal with magic(3), you fail the test. Thus this and all solutions of this class are wrong, marked as correct only because the tests don't offer transposed magic squares as a test. The test vector needs to be fixed so all these solutions show us as not working.

5 Comments