MATLAB: Normalizing data to [-1, 1] range

normalization

Hello
I have a training dataset which is of size NxD and a test dataset which is of size AxD. The rows are the data points and the columns are the features.
Now I would like to transform each feature (column) to be in the range [-1, 1]. Moreover, the scaling of the features in the test set should be done with the parameters estimated on the training set. For example, if I do the standardization by subtracting the mean and dividing the standard deviation, I would calculate the mean and standard deviation on the training set and use them to standardize the test set. The same I want to do now for scaling to the range [-1, 1].
How can this be done?

Best Answer

Why do you think you should divide by the standard deviation????? Just scale to 0-1 like this
range = max(m(:)) - min(m(:));
m01 = (m - min(m(:))) / range;
Then to get to the range of -1 to +1, multiply by 2 and subtract 1:
mOut = 2 * m01 - 1;
If you have the Image Processing Toolbox, you can do it all in just one single line of code because the mat2gray() function does the normalization to the range 0-1 without you having to explicitly find the max and min.
mOut = 2 * mat2gray(m) - 1;