建议先看HOSVD的实现:http://blog.csdn.net/zd836614437/article/details/51193359
1、计算矩阵的秩
输入一个张量:
A(: , : ,1)=[1,0,0;1,0,0;0,0,0];
A(: , : ,2)=[0,0,0;0,1,0;0,0,0];
A(: , : ,3)=[0,0,0;0,0,0;0,0,1];
A=tensor(A);
2、将张量A沿mode-n展开
A1=tenmat(A,1);
A2=tenmat(A,2);
A3=tenmat(A,3);
3、计算每个展开张量的标准差
rows = reshape(A1.data,3*9,1);
std(rows);
mean(rows);
ans =
0.1481
4、计算高斯核矩阵
K1 = KGaussian(A1, A1, 1/0.1418);
K2 = KGaussian(A2, A2, 1/0.1418);
K3 = KGaussian(A3, A3, 1/0.1418);
5、对k进行svd:
[U1,V1,W1]=svd(K1);
[U2,V2,W2]=svd(K2);
[U3,V3,W3]=svd(K3);
6、计算核心张量
S = ttm(A,{U1’,U2’,U3’})
7、计算近似张量
B=ttm(S,{U1,U2,U3})
结果:
B is a tensor of size 3 x 3 x 3
B(:,:,1) =
0.9438 0.0023 0.0015
1.3129 0.0033 0.0020
0.0148 0.0001 0.0016
B(:,:,2) =
0.0023 0.5834 0.0100
0.0033 0.8115 0.0136
0.0001 0.0094 0.0169
B(:,:,3) =
0.0015 0.0100 0.0170
0.0020 0.0136 0.0025
0.0016 0.0169 1.0002
附录:
function K = KGaussian(A, tilde_A, gamma)
% for building kernel data matrix, reduced or full, with Gaussian kernel.
%
% Inputs
% A: full data set.
% tilde_A: can be full or reduced set.
% gamma: width parameter, kernel value = exp{-gamma |A_i-tilde_A_j|_2^2}.
%
% Outputs
% K: kernel data, full or reduced.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r1 = size(A,1); r2=size(tilde_A,1); K=zeros(r1,r2);
for i = 1: r1
for j = 1: r2
dis=A(i,:)-tilde_A(j,:);
K(i,j) = exp( -( gamma * ( dis * dis’ ) ) );
end
end