# how to build a magic matrix manually of even n*n matrix?

13 visualizaciones (últimos 30 días)
dareen el 25 de Mzo. de 2024
Editada: the cyclist el 26 de Mzo. de 2024
i have written an alogorithm magic of odd order yet could not think of one for even order i i am starting to see a pattern for 4 by 4 yet going into 6 by 6 it does seem that i need to take other things into account hints would be appreciated
function res=magicodd(n)
if nargin<1
input("enter a matrix of size 3 or bigger ")
end
if n<3
error("enter a matrix of at least size 3")
end
if mod(n, 2) == 0
error('Order of the magic square must be odd.');
end
row = 1;
res=zeros(n);
col=ceil(n/2);
for num = 1:n^2
res(row,col)=num;
new_row = mod(row - 2, n) + 1;
new_col = mod(col, n) + 1;
% Move up and right
if res(new_row, new_col) == 0
row = new_row;
col = new_col;
else
% Move down
row = mod(row, n) + 1;
end
end
end
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
dareen el 25 de Mzo. de 2024
Editada: dareen el 25 de Mzo. de 2024
i think i found the pattern i will try to implement it should have tried on paper from the start

Iniciar sesión para comentar.

John D'Errico el 25 de Mzo. de 2024
@the cyclist is correct, that this is purely an algorithm question. But you did write code.
Anyway, I'd start here, if I were you:
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
dareen el 26 de Mzo. de 2024
thanks gained a lot of insight

Iniciar sesión para comentar.

### Más respuestas (2)

the cyclist el 25 de Mzo. de 2024
Yours is an algorithm question, not a MATLAB question.
You can find an algorithm for both singly-even and doubly-even magic squares here.
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
dareen el 26 de Mzo. de 2024
thank u

Iniciar sesión para comentar.

the cyclist el 26 de Mzo. de 2024
Editada: the cyclist el 26 de Mzo. de 2024
The method that MATLAB uses to build magic squares is evident if you enter
type magic
at the command line.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Logical en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by