如何将 0.1 和 0.22 之间的数字扩展到更大的距离 0 和 1?
How do I expand the number between 0.1 and 0.22 to larger distance 0 and 1?
我有一个地图 (512x512
),其值介于 0.119
和 0.2499
之间,我想将其用作概率图,因此我需要将此距离扩展到[0,1]
。我该怎么做并将第一张地图的值映射到 [0,1]
?
我想将矩阵中的值映射到新范围,为此我使用了 mapminmax
函数,它对我有用。如果您有其他建议,请告诉我。
谢谢。
手动缩放:不需要工具箱
关注了一些与此相关的帖子 (1, 2, 3, ),
如果你的号码x
当前位于区间[xmin xmax]
,下面的代码完成以下步骤[a]映射到[a b]
:
1. x - xmin
映射 x
到区间 [0 xmax-xmin]
,
2. (x-xmin)./(xmax-xmin)
映射x
到区间[0 1]
;请注意,x = xmin
现在是 0,x = xmax
现在是 1,[b]
3. 将 (x-xmin)./(xmax-xmin)
与 (b-a)
相乘现在将 x
映射到区间 [0 b-a]
(注意逐元素乘法 .*
),
4. 将 a
添加到 ((x-xmin)./(xmax-xmin)).*(b-a)
将该间隔移动到 [a b]
。
请注意,这需要 xmax > xmin
以避免被零除。还需要 b > a
以避免将所有内容映射到 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);
您可以将 min(A(:))
和 max(A(:))
分别与 min(A2(:))
和 max(A2(:))
进行比较。
使用mapminmax()
:需要深度学习工具箱
仔细查看 mapminmax()
的文档(参见 "Algorithms")可以看出上述过程正是该函数所做的。只需将下面的 [ymin ymax]
替换为 [a b]
并与上面提供的 "manually scaling" 方法进行比较。注意 mapminmax()
有一个额外的假设(复制如下)。
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;
所以适当的用法应该是
A3 = mapminmax(A,a,b);
[a] 感谢@S.Kolassa - Reinstate Monica for this excellent answer.
[b]既然你想要[0 1]
,这就是你所需要的。对于那些使用 b > a
寻找间隔 [a b]
的人,我给出了更一般的情况。
我有一个地图 (512x512
),其值介于 0.119
和 0.2499
之间,我想将其用作概率图,因此我需要将此距离扩展到[0,1]
。我该怎么做并将第一张地图的值映射到 [0,1]
?
我想将矩阵中的值映射到新范围,为此我使用了 mapminmax
函数,它对我有用。如果您有其他建议,请告诉我。
谢谢。
手动缩放:不需要工具箱
关注了一些与此相关的帖子 (1, 2, 3,
如果你的号码x
当前位于区间[xmin xmax]
,下面的代码完成以下步骤[a]映射到[a b]
:
1. x - xmin
映射 x
到区间 [0 xmax-xmin]
,
2. (x-xmin)./(xmax-xmin)
映射x
到区间[0 1]
;请注意,x = xmin
现在是 0,x = xmax
现在是 1,[b]
3. 将 (x-xmin)./(xmax-xmin)
与 (b-a)
相乘现在将 x
映射到区间 [0 b-a]
(注意逐元素乘法 .*
),
4. 将 a
添加到 ((x-xmin)./(xmax-xmin)).*(b-a)
将该间隔移动到 [a b]
。
请注意,这需要 xmax > xmin
以避免被零除。还需要 b > a
以避免将所有内容映射到 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);
您可以将 min(A(:))
和 max(A(:))
分别与 min(A2(:))
和 max(A2(:))
进行比较。
使用mapminmax()
:需要深度学习工具箱
仔细查看 mapminmax()
的文档(参见 "Algorithms")可以看出上述过程正是该函数所做的。只需将下面的 [ymin ymax]
替换为 [a b]
并与上面提供的 "manually scaling" 方法进行比较。注意 mapminmax()
有一个额外的假设(复制如下)。
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;
所以适当的用法应该是
A3 = mapminmax(A,a,b);
[a] 感谢@S.Kolassa - Reinstate Monica for this excellent answer.
[b]既然你想要[0 1]
,这就是你所需要的。对于那些使用 b > a
寻找间隔 [a b]
的人,我给出了更一般的情况。