I have a map (512x512
) that has values is between 0.119
and 0.2499
and I want to use it as a probability map so I need to expand this distance to [0,1]
. How can I do it and map the first map's value into [0,1]
?

- 2,856
- 7
- 21
- 41

- 1,255
- 4
- 13
- 26
-
1Can you show the code you have so far and what you have tried? Can you also include an [mre] – shayaan Jan 17 '20 at 08:05
-
Thank you. I think I find the answer. I use mapminmax function and map my values into a new range. – david Jan 17 '20 at 08:08
2 Answers
Manually scaling: No Toolbox Required
Following a number of posts with this (1, 2, 3, 4),
If your numbers x
currently lie on the interval [xmin xmax]
, the code below completes the following steps[a] to map to [a b]
:
1. x - xmin
maps x
to interval [0 xmax-xmin]
,
2. (x-xmin)./(xmax-xmin)
maps x
to the interval [0 1]
; note that x = xmin
are now 0 and x = xmax
are now 1,[b]
3. Multiplying (x-xmin)./(xmax-xmin)
with (b-a)
now maps x
to the interval [0 b-a]
(note element-wise multiplication .*
),
4. Adding a
to ((x-xmin)./(xmax-xmin)).*(b-a)
shifts that interval to [a b]
.
Note this requires xmax > xmin
to avoid division by zero. Also requires b > a
to avoid mapping everything to a
.
xmin = 0.119; % current lowerbound for data
xmax = 0.2499; % current upperbound for data (xmax > xmin)
a = 0; % target lowerbound (under new scale)
b = 1; % target upperbound
A = xmin*ones(512) + (xmax-xmin)*rand(512); % generate example current data
fh=@(x) ((x-xmin)./(xmax-xmin)).*(b-a)+a; % rescaling function
A2 = fh(A);
You can compare min(A(:))
and max(A(:))
with min(A2(:))
and max(A2(:))
, respectively.
Using mapminmax()
: Deep Learning Toolbox Required
A close look at the documentation for mapminmax()
(see "Algorithms") shows the above process is exactly what this function is doing. Just replace [ymin ymax]
below with [a b]
and compare to the above "manually scaling" approach provided. Note mapminmax()
has an additional assumption (copied below).
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;
So the appropriate use would look like
A3 = mapminmax(A,a,b);
[a] Credit to @S.Kolassa - Reinstate Monica for this excellent answer.
[b] Since you desire [0 1]
, this is all you need. I've given the more general case for those seeking interval [a b]
with b > a
.

- 2,856
- 7
- 21
- 41
I want to map my values in a matrix to a new range and I used mapminmax
function for this aim and it works for me. if you have other suggestions please let me know.
Thank you.

- 1,255
- 4
- 13
- 26
-
You're right! The [`mapminmax()`](https://www.mathworks.com/help/deeplearning/ref/mapminmax.html) does this. See my answer for detailed explanation. – SecretAgentMan Jan 17 '20 at 15:05