matlab将矩阵数据重新调整为-1到1

发布于 2024-11-01 01:32:40 字数 386 浏览 1 评论 0原文

可能的重复:
MATLAB:如何将向量标准化/非标准化到范围 [ -1;1]

嗨,刚开始使用Matlab,我想知道如何重新调整矩阵中的数据。 我有一个 N 行 M 列的矩阵,想要将列中的数据重新调整为 -1 和 1 之间。

每列包含的值的范围从 0 - 10,000 到 0 到 1 之间的值,我想标准化为 -1 和 1 之间的原因是这些值将在神经网络中用作基于正弦的变换函数的输入值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝梦月影 2024-11-08 01:32:40

前面的答案都不正确。这就是您需要执行的操作:

[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);

矩阵 normalizedA 的值将介于 -11 之间。

示例:

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

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
半边脸i 2024-11-08 01:32:40

一个简单的解决方案将使用简单的逻辑。假设您打算独立缩放每个列,请执行以下操作:

  1. 减去每列的列最小值。
  2. 将列最大值缩放为 2。
  3. 减去 1。

显然,这将导致每列的最小值为 -1,最大值为 1。执行此操作的代码非常简单。

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

现在,执行上述 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

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
2024-11-08 01:32:40
[m, n] = size(normalizedMatrix)
normalizedMatrix*2-ones(m,n)
[m, n] = size(normalizedMatrix)
normalizedMatrix*2-ones(m,n)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文