forked from andrewssobral/nway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t3core.m
140 lines (116 loc) · 3.06 KB
/
t3core.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
function G=T3core(X,Load,Weights,NonNeg);
%T3CORE calculate Tucker core
%
% G=T3core(X,Load,Weights,NonNeg);
% Calculate a Tucker3 core given X, the loadings, Load
% in vectorized format and optionally Weights. Missing NaN, NonNeg = 1 => nonnegativity
% Copyright (C) 1995-2006 Rasmus Bro & Claus Andersson
% Copenhagen University, DK-1958 Frederiksberg, Denmark, [email protected]
%
% $ Version 2.00 $ May 2001 $ Changed to array notation $ RB $ Not compiled $
for i = 1:length(Load)
Fac(i) = size(Load{i},2);
end
DimX = size(X);
X = reshape(X,DimX(1),prod(DimX(2:end)));
% Convert to old format
ff = [];
for f=1:length(Load)
ff=[ff;Load{f}(:)];
end
Load = ff;
ord=length(DimX);
if length(Fac)==1
Fac=ones(1,ord)*Fac;
end
l_idx=0;
for i=1:ord
l_idx=[l_idx sum(DimX(1:i).*Fac(1:i))];
end
if exist('Weights')~=1,
Weights=0;
elseif isempty(Weights)
Weights = 0;
end
if exist('NonNeg')~=1,
NonNeg=0;
end
if any(isnan(X(:)))
id=find(isnan(X));
M=ones(size(X));
M(id)=zeros(size(id));
X(id)=1000000*ones(size(id));
if prod(size(Weights))==prod(DimX)
%Modified by GT
Weights = reshape(Weights,DimX(1),prod(DimX(2:end)));
%End GT
Weights = Weights.*M;
else
Weights=M;
end
end
if prod(size(Weights))==prod(DimX) % Use weighted approach
LL=reshape(Load(l_idx(1)+1:l_idx(2)),DimX(1),Fac(1));
xtz=zeros(1,prod(Fac));
ztz=zeros(prod(Fac),prod(Fac));
for i=1:DimX(1)
L1=reshape(Load(l_idx(ord)+1:l_idx(ord+1)),DimX(ord),Fac(ord));
L2=reshape(Load(l_idx(ord-1)+1:l_idx(ord)),DimX(ord-1),Fac(ord));
Z=kron(L1,L2);
for ii=ord-2:-1:2
L=reshape(Load(l_idx(ii)+1:l_idx(ii+1)),DimX(ii),Fac(ii));
Z=kron(Z,L);
end
Z=kron(Z,LL(i,:));
ztz=ztz+(Z.*(Weights(i,:)'*ones(1,size(Z,2)) ))'*Z;
xtz=xtz+(X(i,:).*Weights(i,:))*Z;
end
if NonNeg==1;
G=fastnnls(ztz,xtz');
else
G=pinv(ztz)*xtz';
end
else % No weighting
ztz=zeros(prod(Fac),prod(Fac));
L1=reshape(Load(l_idx(ord)+1:l_idx(ord+1)),DimX(ord),Fac(ord));
L1tL1=L1'*L1;
L2=reshape(Load(l_idx(ord-1)+1:l_idx(ord)),DimX(ord-1),Fac(ord-1));
L2tL2=L2'*L2;
ztz=kron(L1tL1,L2tL2);
for o=ord-2:-1:1,
L=reshape(Load(l_idx(o)+1:l_idx(o+1)),DimX(o),Fac(o));
LtL=L'*L;
ztz=kron(ztz,LtL);
end
xtz=zeros(prod(Fac),1);
F=ones(ord,1);
F(1)=0;
for f=1:prod(Fac)
F(1)=F(1)+1;
for ff=1:ord-1
if F(ff)==Fac(ff)+1;
F(ff+1)=F(ff+1)+1;
F(ff)=1;
end
% F runs through all combinations of factors
end
L=reshape(Load(l_idx(1)+1:l_idx(2),:),DimX(1),Fac(1));
cc=L(:,F(1))'*X;
for j=ord:-1:3,
ccc=zeros(prod(DimX(2:j-1)),DimX(j));
ccc(:)=cc;
cc=ccc';
L=reshape(Load(l_idx(j)+1:l_idx(j+1),:),DimX(j),Fac(j));
cc=L(:,F(j))'*cc;
end
L=reshape(Load(l_idx(2)+1:l_idx(2+1),:),DimX(2),Fac(2));
cc=L(:,F(2))'*cc';
xtz(f)=cc;
end
if NonNeg==1,
G=fastnnls(ztz,xtz);
else
G=pinv(ztz)*xtz;
end
end
G = reshape(G,Fac);