forked from andrewssobral/nway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmodel.m
201 lines (176 loc) · 5.12 KB
/
nmodel.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function [Xm]=nmodel(Factors,G,Om);
%NMODEL make model of data from loadings
%
% function [Xm]=nmodel(Factors,G,Om);
%
% This algorithm requires access to:
% 'neye.m'
%
%
% [Xm]=nmodel(Factors,G,Om);
%
% Factors : The factors in a cell array. Use any factors from
% any model.
% G : The core array. If 'G' is not defined it is assumed
% that a PARAFAC model is being established.
% Use G = [] in the PARAFAC case.
% Om : Oblique mode.
% 'Om'=[] or 'Om'=0, means that orthogonal
% projections are requsted. (default)
% 'Om'=1 means that the factors are oblique.
% 'Om'=2 means that the ortho/oblique is solved automatically.
% This takes a little additional time.
% Xm : The model of X.
%
% Using the factors as they are (and the core, if defined) the general N-way model
% is calculated.
% $ Version 2.00 $ May 2001 $ Changed to array notation $ RB $ Not compiled $
% $ Version 1.02 $ Date 17. Apr 1999 $ Not compiled $
% Copyright (C) 1995-2006 Rasmus Bro & Claus Andersson
% Copenhagen University, DK-1958 Frederiksberg, Denmark, [email protected]
%
for i = 1:length(Factors);
DimX(i)=size(Factors{i},1);
end
i = find(DimX==0);
for j = 1:length(i)
DimX(i(j)) = size(G,i(j));
end
if nargin<2, %Must be PARAFAC
Fac=size(Factors{1},2);
G=[];
else
for f = 1:length(Factors)
if isempty(Factors{f})
Fac(f) = -1;
else
Fac(f) = size(Factors{f},2);
end;
end
end
if ~exist('Om')
Om=[];
end;
if isempty(Om)
Om=0;
end;
if size(Fac,2)==1,
Fac=Fac(1)*ones(1,size(DimX,2));
end;
N=size(Fac,2);
if size(DimX,2)>size(Fac,2),
Fac=Fac*ones(1,size(DimX,2));
end;
N=size(Fac,2);
Fac_orig=Fac;
i=find(Fac==-1);
if ~isempty(i)
Fac(i)=zeros(1,length(i));
Fac_ones(i)=ones(1,length(i));
end;
DimG=Fac;
i=find(DimG==0);
DimG(i)=DimX(i);
if isempty(G),
G=neye(DimG);
end;
G = reshape(G,size(G,1),prod(size(G))/size(G,1));
% reshape factors to old format
ff = [];
for f=1:length(Factors)
ff=[ff;Factors{f}(:)];
end
Factors = ff;
if DimG(1)~=size(G,1) | prod(DimG(2:N))~=size(G,2),
help nmodel
fprintf('nmodel.m : ERROR IN INPUT ARGUMENTS.\n');
fprintf(' Dimension mismatch between ''Fac'' and ''G''.\n\n');
fprintf('Check this : The dimensions of ''G'' must correspond to the dimensions of ''Fac''.\n');
fprintf(' If a PARAFAC model is established, use ''[]'' for G.\n\n');
fprintf(' Try to reproduce the error and request help at [email protected]\n');
return;
end;
if sum(DimX.*Fac) ~= length(Factors),
help nmodel
fprintf('nmodel.m : ERROR IN INPUT ARGUMENTS.\n');
fprintf(' Dimension mismatch between the number of elements in ''Factors'' and ''DimX'' and ''Fac''.\n\n');
fprintf('Check this : The dimensions of ''Factors'' must correspond to the dimensions of ''DimX'' and ''Fac''.\n');
fprintf(' You may be using results from different models, or\n');
fprintf(' You may have changed one or more elements in ''Fac'' or ''DimX'' after ''Factors'' have been calculated.\n\n');
fprintf(' Read the information above for information on arguments.\n');
return;
end;
FIdx0=cumsum([1 DimX(1:N-1).*Fac(1:N-1)]);
FIdx1=cumsum([DimX.*Fac]);
if Om==0,
Orthomode=1;
end;
if Om==1,
Orthomode=0;
end;
if Om==2,
Orthomode=1;
for c=1:N,
if Fac_orig(c)~=-1,
A=reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c));
AA=A'*A;
ssAA=sum(sum(AA.^2));
ssdiagAA=sum(sum(diag(AA).^2));
if abs(ssAA-ssdiagAA) > 100*eps;
Orthomode=0;
end;
end;
end;
end;
if Orthomode==0,
Zmi=prod(abs(Fac_orig(2:N)));
Zmj=prod(DimX(2:N));
Zm=zeros(Zmi,Zmj);
DimXprodc0 = 1;
Facprodc0 = 1;
Zm(1:Facprodc0,1:DimXprodc0)=ones(Facprodc0,DimXprodc0);
for c=2:N,
if Fac_orig(c)~=-1,
A=reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c));
DimXprodc1 = DimXprodc0*DimX(c);
Facprodc1 = Facprodc0*Fac(c);
Zm(1:Facprodc1,1:DimXprodc1)=ckron(A',Zm(1:Facprodc0,1:DimXprodc0));
DimXprodc0 = DimXprodc1;
Facprodc0 = Facprodc1;
end;
end;
if Fac_orig(1)~=-1,
A=reshape(Factors(FIdx0(1):FIdx1(1)),DimX(1),Fac(1));
Xm=A*G*Zm;
else
Xm=G*Zm;
end;
elseif Orthomode==1,
CurDimX=DimG;
Xm=G;
newi=CurDimX(2);
newj=prod(CurDimX)/CurDimX(2);
Xm=reshape(Xm',newi,newj);
for c=2:N,
if Fac_orig(c)~=-1,
A=reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c));
Xm=A*Xm;
CurDimX(c)=DimX(c);
else
CurDimX(c)=DimX(c);
end;
if c~=N,
newi=CurDimX(c+1);
newj=prod(CurDimX)/CurDimX(c+1);
else,
newi=CurDimX(1);
newj=prod(CurDimX)/CurDimX(1);
end;
Xm=reshape(Xm',newi,newj);
end;
if Fac_orig(1)~=-1,
A=reshape(Factors(FIdx0(1):FIdx1(1)),DimX(1),Fac(1));
Xm=A*Xm;
end;
end;
Xm = reshape(Xm,DimX);