jus*_*rld 2 matlab matrix formula
在第4 本纸,下式被呈现给零中心核矩阵K尺寸的p:

这是我对应上述公式的代码:
K = K - (1/p)*(K*ones(p,1))*ones(1,p) - (1/p)*ones(p,1)*(ones(1,p)*K) + (1/p^2)*sum(sum(K));
Run Code Online (Sandbox Code Playgroud)
我对代码与论文中实际公式的关系感到困惑.特别是关于最后两个成员- (1/p)*ones(p,1)*(ones(1,p)*K)和(1/p^2)*sum(sum(K)).
有人可以解释一下吗?
好吧,代码不完全正确.第三个术语包括K本文所提出的公式中没有的术语.另外,最后一个术语不会与ee T相乘.但是,在这种情况下可以省略后者,因为MATLAB会自动将标量添加到矩阵中的所有元素.这同样适用于第三项,因此也可以省略.
以下是具有上述简化的行的正确版本:
K = K - (1/p)*(K*ones(p,1))*ones(1,p) - 1/p + (1/p^2)*sum(sum(K))
Run Code Online (Sandbox Code Playgroud)
我们可以通过只调用一次来进一步简化ones,因为ones(p,1)*ones(1,p)它会给你相同的结果ones(p).此外sum(sum(K))可以替换为sum(K(:)).
它看起来像这样:
K = K - (1/p)*K*ones(p) - 1/p + (1/p^2)*sum(K(:))
Run Code Online (Sandbox Code Playgroud)
现在我们可以将它与公式的一对一实现进行比较.因此,我们将e = ones(p,1)用来代表e.为了得到Ë 牛逼,你可以转e用.'.所以公式可以写成如下:
K = K - (1/p)*K*e*e.' - (1/p)*e*e.' + ((e.'*K*e)/p^2)*e*e.'
Run Code Online (Sandbox Code Playgroud)
请注意,e.'*K*e只计算所有元素的总和,K等于sum(K(:)).这是有效的,因为e = ones(p,1).
让我们生成一些样本数据并比较结果:
rng(8); % make it reproducible
p = 3; % size of matrix
K = randi(10,p); % generate random matrix
e = ones(p,1); % generate e-vector
K1 = K - (1/p)*(K*ones(p,1))*ones(1,p) - 1/p + (1/p^2)*sum(sum(K))
K2 = K - (1/p)*K*ones(p) - 1/p + (1/p^2)*sum(K(:))
K3 = K - (1/p)*K*e*e.' - (1/p)*e*e.' + ((e.'*K*e)/p^2)*e*e.'
K4 = K - (1/p)*K*e*e.' - (1/p)*e*e.' + sum(K(:))/p^2
Run Code Online (Sandbox Code Playgroud)
结果如下:
K1 =
8.0000 5.0000 4.0000
9.6667 2.6667 4.6667
9.3333 1.3333 6.3333
K2 =
8.0000 5.0000 4.0000
9.6667 2.6667 4.6667
9.3333 1.3333 6.3333
K3 =
8.0000 5.0000 4.0000
9.6667 2.6667 4.6667
9.3333 1.3333 6.3333
K4 =
8.0000 5.0000 4.0000
9.6667 2.6667 4.6667
9.3333 1.3333 6.3333
Run Code Online (Sandbox Code Playgroud)