Contenuto principale

La traduzione di questa pagina non è aggiornata. Fai clic qui per vedere l'ultima versione in inglese.

magic

Sintassi

Descrizione

M = magic(n) restituisce una matrice n x n costruita dai numeri interi da 1 a n2 con somme uguali di righe e colonne. Per creare un quadrato magico valido, l'ordine n deve essere uno scalare maggiore o uguale a 3.

esempio

Esempi

comprimi tutto

Calcolare il quadrato magico di terzo ordine M.

M = magic(3)
M = 3×3

     8     1     6
     3     5     7
     4     9     2

La somma degli elementi di ciascuna colonna e la somma degli elementi di ciascuna riga sono uguali.

sum(M)
ans = 1×3

    15    15    15

sum(M,2)
ans = 3×1

    15
    15
    15

Esaminare visivamente i pattern delle matrici quadrate magiche con ordini compresi tra 9 e 24 utilizzando imagesc. I pattern mostrano che magic utilizza tre algoritmi diversi, a seconda che il valore di mod(n,4) sia 0, 2 o dispari.

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. Hidden axes object 1 with title 9 contains an object of type image. Hidden axes object 2 with title 10 contains an object of type image. Hidden axes object 3 with title 11 contains an object of type image. Hidden axes object 4 with title 12 contains an object of type image. Hidden axes object 5 with title 13 contains an object of type image. Hidden axes object 6 with title 14 contains an object of type image. Hidden axes object 7 with title 15 contains an object of type image. Hidden axes object 8 with title 16 contains an object of type image. Hidden axes object 9 with title 17 contains an object of type image. Hidden axes object 10 with title 18 contains an object of type image. Hidden axes object 11 with title 19 contains an object of type image. Hidden axes object 12 with title 20 contains an object of type image. Hidden axes object 13 with title 21 contains an object of type image. Hidden axes object 14 with title 22 contains an object of type image. Hidden axes object 15 with title 23 contains an object of type image. Hidden axes object 16 with title 24 contains an object of type image.

Argomenti di input

comprimi tutto

Ordine della matrice, specificato come intero scalare maggiore o uguale a 3. Se n è complesso e non è un intero o uno scalare, magic lo converte in un intero utilizzabile con floor(real(double(n(1)))).

Se si fornisce n inferiore a 3, magic restituisce o un quadrato non magico o i quadrati magici degenerati 1 e [].

Tipi di dati: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char

Funzionalità estese

espandi tutto

Cronologia versioni

Introduzione prima di R2006a

Vedi anche

|