Main Content


Process matrices by mapping row minimum and maximum values to [-1 1]




To rescale data for deep learning workflows, use the Normalization name value pair for the input layer.

[Y,PS] = mapminmax(X,YMIN,YMAX) takes a N-by-Q matrix, X and optionally a minimum and a maximum value for each row of Y, YMIN and YMAX, and returns a N-by-Q matrix, Y, and a process settings that allow consistent processing of values, PS.

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

[Y,PS] = mapminmax(X,FP) takes parameters as a struct: FP.ymin, FP.ymax.

Y = mapminmax('apply',X,PS) returns Y, given X and settings PS.

X = mapminmax('reverse',Y,PS) returns X, given Y and settings PS.

dx_dy = mapminmax('dx_dy',X,Y,PS) returns the reverse derivative.


collapse all

This example shows how to format a matrix so that the minimum and maximum values of each row are mapped to default interval [-1,+1].

x1 = [1 2 4; 1 1 1; 3 2 2; 0 0 0]
[y1,PS] = mapminmax(x1)

Next, apply the same processing settings to new values.

x2 = [5 2 3; 1 1 1; 6 7 3; 0 0 0]
y2 = mapminmax('apply',x2,PS)

Reverse the processing of y1 to get x1 again.

x1_again = mapminmax('reverse',y1,PS)

Input Arguments

collapse all

Matrix you want to process, specified as an N-by-Q matrix.

Minimum value for each row of the output matrix Y, specified as a scalar.

Maximum value for each row of the output matrix Y, specified as a scalar.

Output Arguments

collapse all

Processed matrix, returned as an N-by-Q matrix.

Process settings that allow consistent processing of values, returned as a structure.

More About

collapse all

Normalize Inputs and Targets Using mapminmax

Before training, it is often useful to scale the inputs and targets so that they always fall within a specified range. The function mapminmax scales inputs and targets so that they fall in the range [–1,1]. The following code illustrates how to use this function.

[pn,ps] = mapminmax(p);
[tn,ts] = mapminmax(t);
net = train(net,pn,tn);

The original network inputs and targets are given in the matrices p and t. The normalized inputs and targets pn and tn that are returned will all fall in the interval [–1,1]. The structures ps and ts contain the settings, in this case the minimum and maximum values of the original inputs and targets. After the network has been trained, the ps settings should be used to transform any future inputs that are applied to the network. They effectively become a part of the network, just like the network weights and biases.

If mapminmax is used to scale the targets, then the output of the network will be trained to produce outputs in the range [–1,1]. To convert these outputs back into the same units that were used for the original targets, use the settings ts. The following code simulates the network that was trained in the previous code, and then converts the network output back into the original units.

an = sim(net,pn);
a = mapminmax('reverse',an,ts);

The network output an corresponds to the normalized targets tn. The unnormalized network output a is in the same units as the original targets t.

If mapminmax is used to preprocess the training set data, then whenever the trained network is used with new inputs they should be preprocessed with the minimum and maximums that were computed for the training set stored in the settings ps. The following code applies a new set of inputs to the network already trained.

pnewn = mapminmax('apply',pnew,ps);
anewn = sim(net,pnewn);
anew = mapminmax('reverse',anewn,ts);

For most networks, including feedforwardnet, these steps are done automatically, so that you only need to use the sim command.


It is assumed that X has only finite real values, and that the elements of each row are not all equal. (If xmax=xmin or if either xmax or xmin are non-finite, then y=x and no change occurs.)

y = (ymax-ymin)*(x-xmin)/(xmax-xmin) + ymin;

Version History

Introduced in R2006a