21

I wanna create basic matlab program that normalizes given array of integer in the given range.

  • Inputs are an array [ a1 , a2 , a3 , a4 , a5 , a6 , a7... ], and the range [ x , y ]
  • Output is normalized array.

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.

fiasco
  • 261
  • 1
  • 3
  • 8

3 Answers3

59

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;
Max
  • 3,384
  • 2
  • 27
  • 26
  • 1
    Great solution, but for moments it seemed Python and I copied-pasted without looking :P (and after I upvoted and commented I looked into the title and saw that was for matlab! sorry, though it was very helpful!) – Paulo Oliveira Nov 20 '14 at 10:43
  • What if I haven't the input range? Let's say that I'd like to put a number, for instance 345, in the range [-1,1] – superpuccio Jun 09 '15 at 16:15
  • @superpuccio: ? Can you clarify what you're asking? – Max Jun 09 '15 at 16:28
  • Two linear mappings can be combined into a single linear mapping. This seems like a wasteful answer. – Cris Luengo Aug 16 '18 at 20:40
1

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].

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
0

MATLAB has special function for Normalization, These include in Artificial Neural Network toolbox, which used for normalization input values.

mapminmax

mapminmax processes matrices by normalizing the minimum and maximum values of each row to [YMIN, YMAX].

mapstd

mapstd processes matrices by transforming the mean and standard deviation of each row to ymean and ystd.

more information

PyMatFlow
  • 459
  • 4
  • 8