-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstandardize.m
34 lines (32 loc) · 989 Bytes
/
standardize.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
function [X, Xmean, Xstd] = standardize(X,Xmean, Xstd)
%STANDARDIZE X = standardize(X,X_mean,X_std)
%
% Standardize a matrix to be normally distributed. X is the feature matrix
% of size NxD where N is the number of observations and D is the input
% dimension.
%
% This function can handle the missing data case: all missing data should
% be given the value NaN which will be ignored when performing normalization.
%
% INPUT
% - X : input feature matrix
% - Xmean : mean of the training data (when normalize test data)
% - Xstd : std of the training data (when normalize test data)
%
% OUTPUT
% - X : the standardized matrix
%
% Trung V. Nguyen ([email protected])
% Last updated: 19/11/12
%
if isempty(Xmean) || isempty(Xstd)
Xmean = zeros(1,size(X,2));
Xstd = zeros(1,size(X,2));
for i=1:size(X,2)
x = X(:,i);
Xmean(i) = mean(x(~isnan(x)));
Xstd(i) = std(x(~isnan(x)));
end
end
X = X - repmat(Xmean, size(X,1), 1);
X = X ./ repmat(Xstd, size(X,1), 1);