代码之家  ›  专栏  ›  技术社区  ›  bhushan23

在Matlab中,为什么整个矩阵的L2范数的平方和行/列L2范数的平方和不匹配?

  •  1
  • bhushan23  · 技术社区  · 7 年前

    矩阵L2范数的平方应与所有行/列L2范数的平方和匹配。 参考号: http://mathworld.wolfram.com/L2-Norm.html

    在Matlab中考虑以下随机(4,3)矩阵

    Computed using a = rand(4,3)
    
    0.0400    0.4357    0.9144
    0.5551    0.9048    0.5755
    0.1675    0.1772    0.3001
    0.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中是否有允许我正确执行此操作的选项?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Felix    7 年前

    Matlab对矩阵和向量使用不同的范数。来自Matlab文档 norm :

    n=norm(X)返回矩阵X的2-norm或最大奇异值,近似为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.46
    norms(end+1) = norm(M(:))^2; % 2.87
    norms(end+1) = norm(M(1,:))^2 + norm(M(2,:))^2 + norm(M(3,:))^2 + norm(M(4,:))^2; % 2.87
    norms(end+1) = norm(M(:,1))^2 + norm(M(:,2))^2 + norm(M(:,3))^2; % 2.87
    
    norms
    
    推荐文章