MATLAB - Normalize Data



Matlab is a powerful language which allows you to do a lot of stuff with problem solving, analysis of data, a lot with matrices, vectors , scientific computing etc.

There is a rich source of libraries and other softwares that are used in matlab that makes your life easy. Here we are going to discuss Normalisation of data in Matlab.

The term normalisation deals with organising the data. We are just going to learn the same as how to normalise i.e organise data in matlab.

Normalization is the most common step in data analysis that scales the values of a dataset to a specific range, often between 0 and 1.In Matlab the normalization on dataset can be done using normalize function.The function normalize comes with various options that includes range and type of normalization that can be performed on it.

Syntax

N = normalize(X)
N = normalize(X,dim)
N = normalize(___,method)
N = normalize(___,method,methodtype)

Let us understand each of the syntax with description and examples.

N = normalize(X)

The function will return the z-score data from X, with center as 0 and standard deviation is 1.

Z-score measures the number of standard deviations from above and below the mean value.

The formula for z-score is −

$$\mathrm{Z=\frac{(x\: -\:\mu )}{\sigma }}$$

Here x = observed value or data point.

$\mathrm{\mu}$ is the mean point

$\mathrm{\sigma}$ is the standard deviation.

For the normalize method the input X can be a vector, matrix, multidimensional array or a table.

Following are the ways in which normalize function will operate based on the input given.

  • If the given input X is vector , the nomalize function will operate on all the vector in X.
  • If the given input X is matrix then the normalize function will act on each column in X separately.
  • If the given input X is a multidimensional array, then normalize function will first operate on the first dimension of X whose size is not equal to 1.
  • If the input given to X is a table or timetable the normalize function will operate on each variable of X separately.

Let us see a few examples with the function normalize(X).

Example 1

In the example below we are going to use X as vector

X = 1:10;
N = normalize(X)

On execution in Matlab the output is as follows −

>> X = 1:10;
N = normalize(X)

N =

   -1.4863   -1.1560   -0.8257   -0.4954   -0.1651    0.1651    0.4954    0.8257    1.1560    1.4863

>> 

Example 2

In the example below we are going to use X as a matrix.

X = magic(3)

N = normalize(X)

On execution in Matlab the output is as follows −

>> X = magic(3)

N = normalize(X)

X =

     8     1     6
     3     5     7
     4     9     2


N =

    1.1339   -1.0000    0.3780
   -0.7559         0    0.7559
   -0.3780    1.0000   -1.1339

N = normalize(X,dim)

In above syntax dim is the dimension of matrix X. So as the dimension given the normalize function will operate on that dimension. For example if the dimension given is 2. The normalize function will operate on each row.

Example

X = magic(3)

N = normalize(X, 2)

On execution in Matlab the output is as follows −

>> X = magic(3)

N = normalize(X, 2)

X =

     8     1     6
     3     5     7
     4     9     2

N =

    0.8321   -1.1094    0.2774
   -1.0000         0    1.0000
   -0.2774    1.1094   -0.8321

>>

N = normalize(___,method)

In this syntax the normalization will operate as the method given. The method used can be center, scale , norm, range and zscore.

Example

In this example will use the vector as X and the method scale.

X = 1:5;
N = normalize(X,"scale")

On execution in matlab the output is

>> X = 1:5;
N = normalize(X,"scale")

N =

    0.6325    1.2649    1.8974    2.5298    3.1623

Let us try the same example using range method

>> X = 1:5;
N = normalize(X,"range")

N =

   0    0.2500    0.5000    0.7500    1.0000

>>

N = normalize(___,method,methodtype)

Here the normalize function will act based on the method and methodtype given.

Following are the combination for method and methodtype.

Method Method Type Description
zscore std (default) Get the z-score value.The center data to have mean value as 0 and scale data to have standard deviation as 1.
robust Calculate the z-score value with center data having mean as 0, and scale data having median absolute deviation as 1.
norm Positive numeric scalar (default is 2) For normalization factor p-norm is used where p is a positive integer.
Inf For normalization factor p-norm is used where p is infinity.
scale std (default) Scaling data to have standard deviation as 1.
mad Median absolute deviation as 1 while scaling the data.
first The first element of the data is used for scaling.
iqr Interquatile range is used for scaling data
Numeric array Numeric array is used for scaling data.
Table Table variables are used to scale data.
range 2-element row vector (default is [0 1]) Rescale range of data to [a b], where a < b.
center "mean" (default) Center data to have mean 0.
"median" Center data to have mean 0.
Numeric array Numeric array is used for center data.
Table Table variables are used for center data.

Example 1

In this example will make use of norm method and positive interger value as method type.

X = 1:5;
N = normalize(X,"norm",2)

When you execute above code in matlab command window the output is as follows −

>> X = 1:5;
N = normalize(X,"norm",2)

N =

    0.1348    0.2697    0.4045    0.5394    0.6742

>> 

Example 2

In this example will make use of center as the method and mean as the method type.

X = 1:5;
N = normalize(X,"center","mean")

On execution in matlab command window the output is as follows −

>> X = 1:5;
N = normalize(X,"center","mean")

N =

    -2    -1     0     1     2

>> 
Advertisements