-3

Possible Duplicate:
MATLAB: how to normalize/denormalize a vector to range [-1;1]

Hi, just started using Matlab and I would like to know how to rescale the data in a matrix. I have a matrix of N rows by M columns and want to rescale the data in the columns to be between -1 and 1.

Each column contains values that vary in scale from say 0 - 10,000 to some that are between 0 and 1, the reason I want to normalise to between -1 and 1 as these values will be used in a Neural Network as input values for a transform function that is sine based.

Community
  • 1
  • 1
EdChum
  • 376,765
  • 198
  • 813
  • 562

3 Answers3

5

Neither of the previous answers are correct. This is what you need to do:

[rows,~]=size(A);%# A is your matrix
colMax=max(abs(A),[],1);%# take max absolute value to account for negative numbers
normalizedA=A./repmat(colMax,rows,1);

The matrix normalizedA will have values between -1 and 1.

Example:

A=randn(4)

A =

   -1.0689    0.3252   -0.1022   -0.8649
   -0.8095   -0.7549   -0.2414   -0.0301
   -2.9443    1.3703    0.3192   -0.1649
    1.4384   -1.7115    0.3129    0.6277

normalizedA = 

   -0.3630    0.1900   -0.3203   -1.0000
   -0.2749   -0.4411   -0.7564   -0.0347
   -1.0000    0.8006    1.0000   -0.1906
    0.4885   -1.0000    0.9801    0.7258
abcd
  • 41,765
  • 7
  • 81
  • 98
  • Thanks for the code, I just tried this and it normalised to 0 to 1, not -1 to 1, not sure why there are no negative values. – EdChum Apr 12 '11 at 21:41
  • 1
    ah, I see. you want to scale your elements to `-1,1`. You should take a look at the link Jonas posted in the comments above. That answers your question. – abcd Apr 12 '11 at 21:47
1

A simple solution would use simple logic. Assuming that you mean to scale EACH column independently, do this:

  1. Subtract off the column minimum for each column.
  2. Scale the column maximum to be 2.
  3. Subtract 1.

Clearly this will result in the min for each column to be -1, the max will be 1. Code to do so is simple enough.

A = randn(5,4)   % some random example data
A =
    0.70127      0.20378       0.4085      0.83125
    0.64984     -0.90414      0.67386       1.2022
     1.6843      -1.6584     -0.31735      -1.8981
    -1.3898     -0.89092     -0.23122      -1.2075
    0.72904    -0.095776      0.67517      0.28613

Now, perform the steps above to A.

A = bsxfun(@minus,A,min(A,[],1));
A = bsxfun(@times,A,2./max(A,[],1));
A = A - 1

A =
    0.36043            1      0.46264      0.76071
    0.32697     -0.18989      0.99735            1
          1           -1           -1           -1
         -1      -0.1757     -0.82646     -0.55446
     0.3785      0.67828            1      0.40905
  • Thanks everyone for the suggestions, I will try the above also, I posted on matlab forums [link](http://www.mathworks.com/matlabcentral/newsreader/view_thread/306156) and someone gave me this formula which appears to do what I want for every column: norm_data = bsxfun(@rdivide,data,max(data)/2)-1; – EdChum Apr 13 '11 at 16:52
  • Actually, that only forces EITHER the minimum OR the maximum value to be -1 or 1. It does not do what you asked, unless the maximum value in a column has exactly the same magnitude as the minimum value. But perhaps you don't want to do what you asked for. In fact, if your data actually has negative numbers in it, this will give you VERY unexpected results. –  Apr 14 '11 at 23:30
  • >> data = [-10;0;2];bsxfun(@rdivide,data,max(data)/2)-1 ans = -11 -1 1 –  Apr 14 '11 at 23:30
0
[m, n] = size(normalizedMatrix)
normalizedMatrix*2-ones(m,n)
  • Thanks for quick response my columns have values ranging from 0 to 300,000. The columns are different 'features', when applying your formula they rescale only slightly from before for some of the columns, the higher values ones do not shift much. I think each column would need to have its mean and std deviation caluclated and then applied but I don't immediately know how to do this due to being new to all this. – EdChum Apr 12 '11 at 20:49
  • I should mention that each column has a different range of values, so some range from 0 to 0.5, others 0 to 5 and some are 0 to 700000 – EdChum Apr 12 '11 at 20:55