-
Notifications
You must be signed in to change notification settings - Fork 8
/
ktnormalize.m
executable file
·61 lines (56 loc) · 1.45 KB
/
ktnormalize.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
function [ Y ] = ktnormalize( Y,nrm,nnproj )
if nargin==1
nrm=[];nnproj=[];
elseif nargin==2
nnproj=[];
end
if isempty(nrm)
nrm=2;
end
if isempty(nnproj)
nnproj=true;
end
if strcmpi(class(Y),'ktensor')
I=size(Y);
N=numel(I);
J=length(Y.lambda);
switch nrm
case 0
for n=1:N
ts=0:I(n):(J-1)*I(n);
[temp idx]=max(abs(Y.U{n}));
d=Y.U{n}(idx+ts);
d=sign(d).*max(abs(d),eps);
Y.U{n}=bsxfun(@rdivide,Y.U{n},d);
Y.lambda=Y.lambda.*d';
end
case 1
for n=1:N
d=max(sum(abs(Y.U{n})),eps);
Y.U{n}=bsxfun(@rdivide,Y.U{n},d);
Y.lambda=Y.lambda.*d';
end
case 2
for n=1:N
d=max(sum(Y.U{n}.^2),eps);
Y.U{n}=bsxfun(@rdivide,Y.U{n},d);
Y.lambda=Y.lambda.*d';
end
otherwise
disp('Unsupported norm.');
end
[temp idx]=sort(abs(Y.lambda),'descend');
Y.lambda=Y.lambda(idx);
for n=1:N
Y.U{n}=Y.U{n}(:,idx);
end
%% nonnegative projection
if nnproj
ts=0:I(n):(J-1)*I(n);
[temp idx]=max(abs(Y.U{n}));
d=sign(Y.U{n}(idx+ts));
Y.U{n}=bsxfun(@rdivide,Y.U{n},d);
Y.lambda=Y.lambda.*d';
end
end
end