-
Notifications
You must be signed in to change notification settings - Fork 8
/
datanormalize.m
executable file
·58 lines (55 loc) · 1.54 KB
/
datanormalize.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
function [x xnorm]=datanormalize(x,nor,ori)
% Usage: function x=normalize(x,nor,ori);
% x=normalize(x) for short.
% normalize the matrix x by specified norm(nor) and orientation (ori)
% nor: {2|1|p|inf} norm used. [default 2]
% ori: only valid for a matrix input.
% 1,'col' -- column-wise normalization [default]
% 2,'row' -- row-wise normalization
% Coded by Guoxu Zhou
if nargin==1
nor=[]; ori=[];
elseif nargin==2
ori=[];
end
if isempty(nor) nor=2; end
if isempty(ori) ori='col'; end
switch ori
case {'col',1}
ori=1;
otherwise
ori=2;
end
typeofx=class(x);
switch typeofx
case 'double'
if numel(size(x))~=2
error('For double data, a matrix is expected.');
end
nor=max(nor,0.1);
if nor==2
xnorm=max(sum(x.*x,ori),eps).^0.5;
elseif nor==1
xnorm=max(sum(abs(x),ori),eps);
elseif isinf(nor)
xnorm=max(max(abs(x),[],ori),eps);
else
xnorm=max(sum(abs(x).^nor,ori),eps).^(1/nor);
end
x=bsxfun(@rdivide,x,xnorm);
case 'ktensor'
N=numel(size(x));
for n=1:N
[x.U{n} nu]=datanormalize(x.U{n},nor,'col');
x.lambda=x.lambda.*nu';
end
case 'ttensor'
N=numel(size(x));
for n=1:N
[x.U{n} nu]=datanormalize(x.U{n},nor,'col');
x.core=ttm(x.core,diag(nu),n);
end
otherwise
error('Unsuported data type.');
end
end