function normalized = normalizeCols(datacols, mode)
% Normalizes each column separately. After this, the maximum absolute
% value of each column is 1.0, and other values are scaled linearly by
% the same factor.
%
% If the second parameter is 'energy', then instead of maximum
% absolute value, the energy (sum-of-squared-elements) of each column
% is normalized to 1. Naturally Euclidean norms will be 1 as well.
%

if nargin == 2 && strcmp(mode, 'energy')
    m = sqrt(sum(datacols .* datacols));
    scal = ones(size(m)) ./ m;
    normalized = repmat(scal, size(datacols,1), 1) .* datacols;
    return
end
if nargin == 2 && (~strcmp(mode, 'energy'))
    error('Unknown arguments');
end


m = max(abs(datacols));
if m == 0, m = 1; end;  % In case all elems are 0, don't even try scaling
    
scal = ones(size(m)) ./ m;
normalized = repmat(scal, size(datacols,1), 1) .* datacols;
