function [nfea_tr, nfea_ts] = normalize(fea_tr, fea_ts, cn)
% normalize, to make all dimension lie in [0,1]

if nargin ~= 3
    disp('normalize(fea_tr, fea_ts, cn)');
    return;
end

% normalize to each class
trnum = size(fea_tr, 2) / cn;
tsnum = size(fea_ts, 2) / cn;

nfea_tr = [];
nfea_ts = [];

for k = 1:cn
    ftr = fea_tr(:, (k-1)*trnum+1:k*trnum);
    fts = fea_ts(:, (k-1)*tsnum+1:k*tsnum);
    
    min_tr = min(ftr, [], 2);
    max_tr = max(ftr, [], 2);
    trmin = repmat(min_tr, 1, trnum);
    trmax = repmat(max_tr, 1, trnum);
    nfea_tr = [nfea_tr, (ftr - trmin) ./ (trmax - trmin)];
    tsmin = repmat(min_tr, 1, tsnum);
    tsmax = repmat(max_tr, 1, tsnum);
    nfea_ts = [nfea_ts, (fts - tsmin) ./ (tsmax - tsmin)];
end
