M = magic(n) returns an
n-by-n matrix constructed from the
integers 1 through
n2 with equal row and column sums.
The order n must be a scalar greater than or equal to
3 in order to create a valid magic square.
Visually examine the patterns in magic square matrices with orders between 9 and 24 using imagesc. The patterns show that magic uses three different algorithms, depending on whether the value of mod(n,4) is 0, 2, or odd.
for n = 1:16
subplot(4,4,n)
ord = n+8;
m = magic(ord);
imagesc(m)
title(num2str(ord))
axis equal
axis offend
Matrix order, specified as a scalar integer greater than or equal to 3. If
n is complex, not an integer, or not scalar, then
magic converts it into a usable integer with
floor(real(double(n(1)))).
If you supply n less than 3, then
magic returns either a nonmagic square, or the
degenerate magic squares 1 and
[].
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window.
Web browsers do not support MATLAB commands.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.