-
Notifications
You must be signed in to change notification settings - Fork 45
/
tensor_hosvd.m
45 lines (39 loc) · 1.06 KB
/
tensor_hosvd.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
%% [core,U] = tensor_hosvd(T,t,r)
%
%%% Normal HoSVD
% [core,U] = tensor_hosvd(T)
% [core,U] = tensor_hosvd(T,0)
% [core,U] = tensor_hosvd(T,[0 0 0])
%
%%% Truncate mode-3 basis matrice (keep the first 2 eigenvalues)
% [core,U] = tensor_hosvd(T,[0 0 2])
%
%%% Perform mode-3 rank-2 partial svd
% [core,U] = tensor_hosvd(T,0,[0 0 2])
%
function [core,U] = tensor_hosvd(T,t,r)
core = T;
if(nargin < 3) r = zeros(1,ndims(T)); end
if(nargin < 2) t = zeros(1,ndims(T)); end
if(t == 0) t = zeros(1,ndims(T)); end
if(r == 0) r = zeros(1,ndims(T)); end
for i = 1:ndims(T)
M{i} = double(tenmat(T,i));
%M{i} = unfolding(double(T),i);
%%% perform partial svd
if(r(i) > 0)
[U{i},S{i},V{i}] = svds(M{i},r(i),'L');
else
[U{i},S{i},V{i}] = svd(M{i});
end
%%% truncate basis matrice
if(t(i) > 0)
U{i}(:,t(i)+1:end) = 0;
end
%[m n] = size(U{i});
%s(i) = n;
%core = tensor(folding(U{i}'*unfolding(double(core),i), i, s));
%core = double(ttm(tensor(core),U{i},i,'t'));
core = ttm(core,U{i}',i);
end
end