矩阵的L2范数的平方应该等于所有行/列的L2范数的平方之和。参考:http://mathworld.wolfram.com/L2-Norm.html
考虑以下在Matlab中生成的随机(4,3)矩阵
Computed using a = rand(4,3)0.0400 0.4357 0.91440.5551 0.9048 0.57550.1675 0.1772 0.30010.4189 0.0403 0.2407
整个矩阵的L2范数为:
norm(a1)^2 = 2.7806
按列计算的L2范数平方之和为:
norm(a1(:,1))^2 + norm(a1(:,2))^2 + norm(a1(:,3))^2 = 2.9337
按行计算的L2范数平方之和为:
norm(a1(1,:))^2 + norm(a1(2,:))^2 + norm(a1(3,:))^2 = 2.2214
而在Python(numpy)中,这两者是匹配的:
a = np.random.rand(4,3)array([[ 0.91033221, 0.9082118 , 0.6864961 ], [ 0.15157616, 0.70232112, 0.06709103], [ 0.61008197, 0.15648347, 0.02693866], [ 0.53646277, 0.22186601, 0.77530143]])
整个矩阵的L2范数
numpy.linalg.norm(a)**2 = 3.9810836846898465
按行计算的L2范数平方之和:
numpy.linalg.norm(a[0])**2 + numpy.linalg.norm(a[1])**2 + numpy.linalg.norm(a[2])**2 + numpy.linalg.norm(a[3])**2 = 3.9810836846898465
Matlab是否在使用较低的精度进行运算,从而导致整个矩阵的范数与按行/列计算的范数之间累积了差异?
Matlab中有没有选项可以让我正确地进行这种计算?
回答:
Matlab对矩阵和向量使用了不同的范数。根据Matlab的norm
函数文档:
n = norm(X) 返回矩阵X的2-范数或最大奇异值,大约等于max(svd(X))。
因此,要得到与您按行和按列计算相似的结果,您必须将矩阵向量化。
M =[0.0400, 0.4357, 0.9144; 0.5551, 0.9048, 0.5755; 0.1675, 0.1772, 0.3001; 0.4189, 0.0403, 0.2407 ];norms = [];norms(end+1) = norm(M)^2; % 2.46norms(end+1) = norm(M(:))^2; % 2.87norms(end+1) = norm(M(1,:))^2 + norm(M(2,:))^2 + norm(M(3,:))^2 + norm(M(4,:))^2; % 2.87norms(end+1) = norm(M(:,1))^2 + norm(M(:,2))^2 + norm(M(:,3))^2; % 2.87norms