Main Content

magic

Sintaxis

Descripción

ejemplo

M = magic(n) devuelve una matriz de n por n construida a partir de los enteros 1 a n2 cuyas filas y columnas sumen lo mismo. El orden n debe ser un escalar igual a o mayor que 3 para crear un cuadrado mágico válido.

Ejemplos

contraer todo

Calcule el cuadrado mágico de tercer orden M.

M = magic(3)
M = 3×3

     8     1     6
     3     5     7
     4     9     2

La suma de los elementos de cada columna y la suma de los elementos de cada fila son iguales.

sum(M)
ans = 1×3

    15    15    15

sum(M,2)
ans = 3×1

    15
    15
    15

Examine visualmente los patrones en las matrices de un cuadrado mágico con órdenes entre 9 y 24 utilizando imagesc. Los patrones muestran que magic utiliza tres algoritmos diferentes, en función de si el valor de mod(n,4) es 0, 2 o impar.

for n = 1:16
    subplot(4,4,n)
    ord = n+8;
    m = magic(ord);
    imagesc(m)
    title(num2str(ord))
    axis equal
    axis off
end

Figure contains 16 axes objects. Axes object 1 with title 9 contains an object of type image. Axes object 2 with title 10 contains an object of type image. Axes object 3 with title 11 contains an object of type image. Axes object 4 with title 12 contains an object of type image. Axes object 5 with title 13 contains an object of type image. Axes object 6 with title 14 contains an object of type image. Axes object 7 with title 15 contains an object of type image. Axes object 8 with title 16 contains an object of type image. Axes object 9 with title 17 contains an object of type image. Axes object 10 with title 18 contains an object of type image. Axes object 11 with title 19 contains an object of type image. Axes object 12 with title 20 contains an object of type image. Axes object 13 with title 21 contains an object of type image. Axes object 14 with title 22 contains an object of type image. Axes object 15 with title 23 contains an object of type image. Axes object 16 with title 24 contains an object of type image.

Argumentos de entrada

contraer todo

Orden de la matriz, especificado como escalar entero igual a o mayor que 3. Si n es complejo y no es un entero ni un escalar, magic lo convierte en un entero útil con floor(real(double(n(1)))).

Si proporciona n menor que 3, magic devuelve un cuadrado no mágico o los cuadrados mágicos degenerados 1 y [].

Tipos de datos: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char

Capacidades ampliadas

Historial de versiones

Introducido antes de R2006a

Consulte también

|