开发者

matlab rescale matrix data to -1 to 1 [duplicate]

开发者 https://www.devze.com 2023-02-24 09:19 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: MATLAB: how to normalize/denormalize a vector to range [-1;1]
This question already has answers here: Closed 11 years ago.

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.

开发者_如何学Python

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.


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


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


[m, n] = size(normalizedMatrix)
normalizedMatrix*2-ones(m,n)
0

精彩评论

暂无评论...
验证码 换一张
取 消