How to normalize data between 0 and 1

210 ビュー (過去 30 日間)
Med Future
Med Future 2022 年 3 月 3 日
コメント済み: Walter Roberson 2025 年 2 月 24 日
Hello everyone, i hope you are doing well.
I have the following data.
I want to normalize the data between 0 and 1
How can i do that

回答 (3 件)

Simon Chan
Simon Chan 2022 年 3 月 3 日
Another option is use function rescale

Davide Masiello
Davide Masiello 2022 年 3 月 3 日
Given the matrix A that you want to normalise, you could write
A = A/max(A(:));
  1 件のコメント
Image Analyst
Image Analyst 2022 年 3 月 3 日
He wants the output to go between 0 and 1. Your code will not put the min at 0 unless it is already 0. The easiest solution is to just use
A = rescale(A, 0, 1);

サインインしてコメントする。


Sinan Islam
Sinan Islam 2025 年 2 月 23 日
X_max = max(X);
X_min = min(X);
X_scaled = (X - X_min) ./ (X_max - X_min); % scales X to [0, 1]
  1 件のコメント
Walter Roberson
Walter Roberson 2025 年 2 月 24 日
That does not work if X consists of identical values
X = [1 1 1 1]
X = 1×4
1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X_max = max(X);
X_min = min(X);
X_scaled = (X - X_min) ./ (X_max - X_min) % scales X to [0, 1]
X_scaled = 1×4
NaN NaN NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rescale(X)
ans = 1×4
0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The code also produces questionable values if the maximum is inf but the points are not all identical
X = [1 2 inf]
X = 1×3
1 2 Inf
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X_max = max(X);
X_min = min(X);
X_scaled = (X - X_min) ./ (X_max - X_min) % scales X to [0, 1]
X_scaled = 1×3
0 0 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rescale(X)
ans = 1×3
NaN NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If one of the input values is -inf then the code produces all NaN

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by