magic
説明
例
3 次の魔方陣
3 次の魔方陣 M
を計算します。
M = magic(3)
M = 3×3
8 1 6
3 5 7
4 9 2
各列の要素の合計と各行の要素の合計は同じです。
sum(M)
ans = 1×3
15 15 15
sum(M,2)
ans = 3×1
15
15
15
魔方陣の可視化
imagesc
を使用して、次数が 9 ~ 24 である魔方陣行列のパターンを視覚的に調べます。パターンは、mod(n,4)
の値が 0、2、または奇数のいずれであるかに応じて magic
が 3 つの異なるアルゴリズムを使用していることを示します。
for n = 1:16 subplot(4,4,n) ord = n+8; m = magic(ord); imagesc(m) title(num2str(ord)) axis equal axis off end
入力引数
n
— 行列の次数
スカラー整数
行列の次数。3 以上のスカラー整数として指定します。n
が整数やスカラーではなく複素数である場合、magic
はこの値を floor(real(double(n(1))))
により使用可能な整数に変換します。
n
が 3
より小さい場合、magic
は魔方陣ではない行列、または縮退した魔方陣 1
や []
を返します。
データ型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
拡張機能
C/C++ コード生成
MATLAB® Coder™ を使用して C および C++ コードを生成します。
スレッドベースの環境
MATLAB® の backgroundPool
を使用してバックグラウンドでコードを実行するか、Parallel Computing Toolbox™ の ThreadPool
を使用してコードを高速化します。
この関数はスレッドベースの環境を完全にサポートしています。詳細については、スレッドベースの環境での MATLAB 関数の実行を参照してください。
バージョン履歴
R2006a より前に導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)