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

377 views (last 30 days)
Avi on 8 Sep 2014
Answered: Steven Lord on 1 Aug 2019
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 on 8 Sep 2014
Edited: José-Luis on 8 Sep 2014
Once you have your result:
scaledl = scaledl.*2 - 1;
Or directly:
result = -1 + 2.*(data - min(data))./(max(data) - min(data));
##### 2 CommentsShowHide 1 older comment
Udaya Rongala on 1 Aug 2019
Thanks @Robert Hus

### More Answers (2)

Steven Lord on 1 Aug 2019
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 on 3 May 2015
Edited: JAY R on 3 May 2015
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