1. 程式人生 > >【 MATLAB 】norm ( Vector and matrix norms )(向量範數以及矩陣範數)

【 MATLAB 】norm ( Vector and matrix norms )(向量範數以及矩陣範數)

norm

Vector and matrix norms


Syntax

n = norm(v)

n = norm(v,p)

n = norm(X)

n = norm(X,p)

n = norm(X,'fro')


Description

n = norm(v)返回向量v的歐幾里德範數。該範數也稱為2範數,向量幅度或歐幾里德長度。

n = norm(v,p)返回廣義向量p範數。

n = norm(X)返回矩陣X的2範數或最大奇異值,其近似為max(svd(X))。

n = norm(X,p)返回矩陣X的p範數,其中p為1,2或Inf:

  • 如果p = 1,則n是矩陣的最大絕對列和。

  • 如果p = 2,則n近似為max(svd(X))。 這相當於norm(X)。

  • 如果p = Inf,那麼n是矩陣的最大絕對行和。

n = norm(X,'fro')返回矩陣X的Frobenius範數。


有關範數的基礎知識,見博文:【 MATLAB 】範數的必備基礎知識

下面舉例說明:

Vector Magnitude(向量幅度)

%Create a vector and calculate the magnitude.

v = [1 -2 3];
n = norm(v)
% n = 3.7417

1-Norm of Vector

clc
clear
close all

% Calculate the 1-norm of a vector, which is the sum of the element magnitudes.

X = [-2 3 -1];
n = norm(X,1)
% n = 6

Euclidean Distance Between Two Points

clc
clear
close all

% Calculate the distance between two points as the norm of the difference between the vector elements.
% 
% Create two vectors representing the (x,y) coordinates for two points on the Euclidean plane.

a = [0 3];
b = [-2 1];
% Use norm to calculate the distance between the points.

d = norm(b-a)

d =

    2.8284
幾何上,兩點之間的距離:


2-Norm of Matrix

clc
clear
close all

% Calculate the 2-norm of a matrix, which is the largest singular value.

X = [2 0 1;-1 1 0;-3 3 0];
n = norm(X)
% n = 4.7234

Frobenius Norm of Sparse Matrix

clc
clear
close all

% 使用'fro'計算稀疏矩陣的Frobenius範數,該範數計算列向量的2範數S(:)。

S = sparse(1:25,1:25,1);
n = norm(S,'fro')
% n = 5