X為向量，
n = norm(X)       % 即求歐幾理德範數(Euclidean norm)

n = norm(X, inf)  % 即求無窮範數(maximum norm)

n = norm(X, 1)    % 即求1-範數

n = norm(X, -inf) % 即求向量X的元素的絕對值的最小值

n = norm(X, p)    % 即求p-範數，所以norm(X, 2) = norm(X)

A為矩陣，

n = norm(A)       % 求歐幾理德範數(Euclidean norm)，等於A矩陣的最大奇異值(singular value)

n = norm(A, 1)   % 求A的列向量的1-範數中的最大值

n = norm(A, 2)   % 求A的歐幾理德範數(Euclidean norm)，等同於norm(A)

n = norm(A, inf)  % 求行範數，等同於A的行向量的1-範數中的最大值，即: max(sum(abs(A')))

n = norm(A, 'fro') % 求A矩陣的Frobenius範數

a = magic(3)

sum(sum(abs(a)^4))^(1/4)

a =

8   1   6

3   5   7

4   9   2

ans =

19.7411

p階範數公式：

Matlab的magic square函數

M = magic(n)

returns an n-by-n matrix constructed from the integers 1 through n^2 with equal row and column sums.

The order n must be a scalar greater than or equal to 3.

Tips:

A magic square, scaled by its magic sum, is doubly stochastic.

Examples:

The magic square of order 3 is

```M = magic(3)

M =

8    1    6
3    5    7
4    9    2```

This is called a magic square because the sum of the elements in each column is the same.

``` sum(M) =

15    15    15```

And the sum of the elements in each row, obtained by transposing twice, is the same.

``` sum(M')' =

15
15
15```

This is also a special magic square because the diagonal elements have the same sum.

```sum(diag(M)) =

15```

The value of the characteristic sum for a magic square of order n is

```sum(1:n^2)/n
```

which, when n = 3, is 15.

Algorithm:

There are three different algorithms:

• n odd

• n even but not divisible by four

• n divisible by four

To make this apparent, type

```for n = 3:20
A = magic(n);
r(n) = rank(A);
end```

For n odd, the rank of the magic square is n. For n divisible by 4, the rank is 3. For n even but not divisible by 4, the rank is n/2 + 2.

```[(3:20)',r(3:20)']
ans =
3     3
4     3
5     5
6     5
7     7
8     3
9     9
10     7
11    11
12     3
13    13
14     9
15    15
16     3
17    17
18    11
19    19
20     3```

Plotting A for n = 18, 19, 20 shows the characteristic plot for each category.

Limitations:

If you supply n less than 3magic returns either a nonmagic square, or else the degenerate magic squares 1 and [].

Totui

Totui