MATLAB – How to scale/normalize values in a matrix to be between 0 and 1

I hate that I have to keep looking this up…

Here’s how to scale or normalize your numbers in MATLAB so they lie between 0 and 1.
Change the number of mins and maxs depending on the dimensionality of your matrix.

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

All of scaledI values are now between 0 and 1.

3 thoughts on “MATLAB – How to scale/normalize values in a matrix to be between 0 and 1”

Questions/comments? If you just want to say thanks, consider sharing this article or following me on Twitter!