# How to normalize values in a matrix to be between 0 and 1?

316 views (last 30 days)
Sahar abdalah on 8 Apr 2015
Commented: Tubi on 22 Mar 2018
I have a matrix Ypred that contain negative values and I want to normalize this matrix between 0 and 1
Ypred=[-0.9630 -1.0107 -1.0774
-1.2075 -1.4164 -1.2135
-1.0237 -1.0082 -1.0714
-1.0191 -1.3686 -1.2105];
I'm new in matlab, please help me, there is a matlab function or toolbox that can do this? thanks
##### 2 CommentsShowHide 1 older comment
Sahar abdalah on 8 Apr 2015
I want to divide by norm of row to make positive values

Jos (10584) on 8 Apr 2015
Edited: Jos (10584) on 8 Apr 2015
This can be simply done in a two step process
1. subtract the minimum
2. divide by the new maximum
normA = A - min(A(:))
normA = normA ./ max(normA(:)) % *
note that A(:) makes A into a long list of values. Otherwise min(A) would not return a single value ... Try fro yourself!
• Edited after comment ...
##### 2 CommentsShowHide 1 older comment
Jos (10584) on 8 Apr 2015
Sorry! The second line of code is wrong ;-) It should read
normA = normA ./ max(normA(:))

James Tursa on 8 Apr 2015
NormRows = sqrt(sum(Ypred.*Ypred,2));
Ynorm = bsxfun(@rdivide,abs(Ypred),NormRows);
Tubi on 22 Mar 2018
Many Thanks John, I believe you are right in your suggestion since they are mostly common MATLAB commands and functions.

Sahar abdalah on 9 Apr 2015
thank you for your answers. I used both codes and I found two different result. what is the result that I can use?
normA = Ypred - min(Ypred(:))
normA = normA ./ max(normA(:))
normA =
1.0000 0.8948 0.7477
0.4607 0 0.4475
0.8661 0.9003 0.7609
0.8763 0.1054 0.4541
NormRows = sqrt(sum(Ypred.*Ypred,2));
Ynorm = bsxfun(@rdivide,abs(Ypred),NormRows);
Ynorm =
0.5461 0.5731 0.6110
0.5435 0.6375 0.5462
0.5712 0.5625 0.5978
0.4871 0.6542 0.5786
##### 2 CommentsShowHide 1 older comment
Sahar abdalah on 9 Apr 2015
ok thanks

c4ndc on 12 Aug 2017
Hello, What is the name of this norm in the accepted answer? (Euclidean, Frobenius etc.)
Jan on 12 Aug 2017
The accepted answer does not contain a norm at all, but a "normalization". A matrix norm would reply a scalar, the normalization replies a matrix with the same size, but with shifted and scaled values.

### Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by