MATLAB Answers

Is there any way to normalize any matrix values to be between (-1) and (1)?

16 views (last 30 days)
Sarah A
Sarah A on 12 Jan 2019
Commented: Walter Roberson on 13 Jan 2019
Hello,
Is there any way to normalize any matrix values to be exactly between (-1) and (1)? I attached the matrix.
regards,

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 12 Jan 2019
You can use mat2gray(), which normalizes data from 0-1 then just scale and subtract 1 to get into the range -1 to +1:
M = 2*mat2gray(M)-1;
A nice single-line solution.

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 13 Jan 2019
M = (2*mat2gray(M)-1) * (1-eps);
the 1-eps contracts from [-1 1] to [-(1-eps) +(1-eps)] which is (-1, 1) to within double precision.
Sarah A
Sarah A on 13 Jan 2019
No it is still between -1.0000 and 1.0000, is there any way to fix that ?
Walter Roberson
Walter Roberson on 13 Jan 2019
They would display as -1.0000 and 1.0000 especially with short format in effect, but if you take 1-max(M(:)) and min(M(:))+1 then you will see non-zero residues.

Sign in to comment.

More Answers (2)


Steven Lord
Steven Lord on 12 Jan 2019
Use the normalize function with the 'range' method if you're using release R2018a or later.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!