I wanna create basic matlab program that normalizes given array of integer in the given range.
But in everywhere, i see the normalization in the range of [0,1] or [-1,1]. Can't find variable range normalization. I will be grateful if you write the matlab code or the formula for variable range.
Thank you for ideas.
If you want to normalize to [x, y]
, first normalize to [0, 1]
via:
range = max(a) - min(a);
a = (a - min(a)) / range;
Then scale to [x,y]
via:
range2 = y - x;
a = (a * range2) + x;
Putting it all together:
function normalized = normalize_var(array, x, y)
% Normalize to [0, 1]:
m = min(array);
range = max(array) - m;
array = (array - m) / range;
% Then scale to [x,y]:
range2 = y - x;
normalized = (array*range2) + x;
Starting from R2017b, MATLAB has this function named rescale
which does exactly this.
i.e. if you want to rescale array
to the interval [x, y]
then:
normalized_array = rescale(array, x, y);
If x
and y
are not specified then array
gets normalized to the interval [0,1]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With