语法:
X = tt_fac_to_vec(A)
converts a cell array of factor matrices A to a vector by vectorizing each matrix and stacking them.
函数功能就是将因子矩阵构成的细胞数组A 通过向量化每个数组 并且叠放一起 转化成一个向量。
例子:
%生成随机的2*3*2的张量
>> a=tensor(rand(2,3,2))
a is a tensor of size 2 x 3 x 2
a(:,:,1) =
0.4908 0.2657 0.0638
0.7251 0.2206 0.5931
a(:,:,2) =
0.7621 0.5358 0.2929
0.1999 0.6221 0.2121
%对张量进行cp分解
>> b=cp_als(a,2)CP_ALS:
Iter 1: f = 6.623389e-01 f-delta = 6.6e-01
Iter 2: f = 7.614281e-01 f-delta = 9.9e-02
Iter 3: f = 8.049933e-01 f-delta = 4.4e-02
Iter 4: f = 8.154584e-01 f-delta = 1.0e-02
Iter 5: f = 8.205675e-01 f-delta = 5.1e-03
Iter 6: f = 8.232252e-01 f-delta = 2.7e-03
Iter 7: f = 8.246533e-01 f-delta = 1.4e-03
Iter 8: f = 8.254496e-01 f-delta = 8.0e-04
Iter 9: f = 8.259128e-01 f-delta = 4.6e-04
Iter 10: f = 8.261940e-01 f-delta = 2.8e-04
Iter 11: f = 8.263714e-01 f-delta = 1.8e-04
Iter 12: f = 8.264871e-01 f-delta = 1.2e-04
Iter 13: f = 8.265644e-01 f-delta = 7.7e-05
Final f = 8.265644e-01
b is a ktensor of size 2 x 3 x 2
b.lambda = [ 1.5422 0.72091 ]
b.U{1} =
0.7211 0.0195
0.6928 0.9998
b.U{2} =
0.7293 0.8234
0.5599 -0.1567
0.3932 0.5455
b.U{3} =
0.4739 0.7184
0.8806 -0.6956
%因为因子矩阵在b.U的细胞数组总存储,使用tt_fac_to_vec函数对其进行向量化。
>> c=tt_fac_to_vec(b.U)
c =
0.7211
0.6928
0.0195
0.9998
0.7293
0.5599
0.3932
0.8234
-0.1567
0.5455
0.4739
0.8806
0.7184
-0.6956