필터 지우기
필터 지우기

How to scale/normalize values in a matrix to be between -1 and 1

조회 수: 94 (최근 30일)
Avi
Avi 2014년 9월 8일
댓글: Steven Lord 2023년 6월 1일
I found the script that scale/normalize values in a matrix to be between 0 and 1
I = [ 1 2 3; 4 5 6]; % Some n x m matrix I that contains unscaled values.
scaledI = (I-min(I(:))) ./ (max(I(:)-min(I(:))));
min(scaledI(:)) % the min is 0
max(scaledI(:)) % the max 1
Was wondering if anyone could help me normalize values in matrix between -1 and +1 Thanks

채택된 답변

José-Luis
José-Luis 2014년 9월 8일
편집: José-Luis 2014년 9월 8일
Once you have your result:
scaledl = scaledl.*2 - 1;
Or directly:
result = -1 + 2.*(data - min(data))./(max(data) - min(data));
  댓글 수: 2
Robert Hus
Robert Hus 2017년 4월 13일
편집: Robert Hus 2017년 4월 13일
if you have an unequal spread of your data between positive and negative numbers, than the above solution may revert the sign of your array mean. To honour the original spread of positive and negative values (e.g if your smallest negative number is -20 and your largest positive number is +40) you can use the following function. Using this function the -20 will become -0.5 and the +40 will be +1. The solution above has the -20 equates to -1 and +40 to +1.
function norm_value = normalised_diff( data )
% Normalise values of an array to be between -1 and 1
% original sign of the array values is maintained.
if abs(min(data)) > max(data)
max_range_value = abs(min(data));
min_range_value = min(data);
else
max_range_value = max(data);
min_range_value = -max(data);
end
norm_value = 2 .* data ./ (max_range_value - min_range_value);
end

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Steven Lord
Steven Lord 2019년 8월 1일
If you're using release R2018a or later, use the normalize function. Specify 'range' as the method and the range to which you want the data normalized (in this case [-1, 1]) as the methodtype.
x = 5*rand(1, 10)
n = normalize(x, 'range', [-1 1])
[minValue, maxValue] = bounds(n) % Should return -1 and 1

JAY R
JAY R 2015년 5월 3일
편집: JAY R 2015년 5월 3일
function data = normalize(d)
% the data is normalized so that max is 1, and min is 0
data = (d -repmat(min(d,[],1),size(d,1),1))*spdiags(1./(max(d,[],1)-min(d,[],1))’,0,size(d,2),size(d,2));
taken from here
  댓글 수: 2
Syed
Syed 2023년 6월 1일
i need denormalized command
Steven Lord
Steven Lord 2023년 6월 1일
Suppose I told you that I had a normalized data set. Here it is.
x = [0 1];
What was the un-normalized data that was used to generate x? Any of these sets could have resulted in this normalized data.
y1 = [0 1];
y2 = [-1 1];
y3 = [42 1e6];
normalize(y1, 'range', [0 1])
ans = 1×2
0 1
normalize(y2, 'range', [0 1])
ans = 1×2
0 1
normalize(y3, 'range', [0 1])
ans = 1×2
0 1
So what additional information do you have that would let you "denormalize" x to generate a specific one of y1, y2, or y3?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by