-
Notifications
You must be signed in to change notification settings - Fork 0
/
NormalizeFea.m
61 lines (56 loc) · 1.89 KB
/
NormalizeFea.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 fea = NormalizeFea(fea,row)
% if row == 1, normalize each row of fea to have unit norm;
% if row == 0, normalize each column of fea to have unit norm;
%
% version 3.0 --Jan/2012
% version 2.0 --Jan/2012
% version 1.0 --Oct/2003
%
% Written by Deng Cai (dengcai AT gmail.com)
%
if ~exist('row','var')
row = 1;
end
if row
nSmp = size(fea,1); % number of rows
feaNorm = max(1e-14,full(sum(fea.^2,2))); % sum(aa,2) returns a vector that each element is the sum of the row
% sum(aa,2) sum of the row
% and returns a column vector
% feaNorm: is a vector, where
% each element is the sum of
% squares of each row.
fea = spdiags(feaNorm.^-.5,0,nSmp,nSmp)*fea; %Sparse matrix formed from diagonals
else
nSmp = size(fea,2);
feaNorm = max(1e-14,full(sum(fea.^2,1))');
fea = fea*spdiags(feaNorm.^-.5,0,nSmp,nSmp);
end
return;
%
% if row
% [nSmp, mFea] = size(fea); % nSmp number of samples
% % mFea number of feature
% if issparse(fea)
% fea2 = fea';
% feaNorm = mynorm(fea2,1);
% for i = 1:nSmp
% fea2(:,i) = fea2(:,i) ./ max(1e-10,feaNorm(i));
% end
% fea = fea2';
% else
% feaNorm = sum(fea.^2,2).^.5;
% fea = fea./feaNorm(:,ones(1,mFea));
% end
% else
% [mFea, nSmp] = size(fea);
% if issparse(fea)
% feaNorm = mynorm(fea,1);
% for i = 1:nSmp
% fea(:,i) = fea(:,i) ./ max(1e-10,feaNorm(i));
% end
% else
% feaNorm = sum(fea.^2,1).^.5;
% fea = fea./feaNorm(ones(1,mFea),:);
% end
% end
%