문제
한 숫자 범위를 다른 숫자로 변환하여 비율을 유지하려고합니다. 수학은 나의 강력한 요점이 아닙니다.
포인트 값의 범위가 -16000.00에서 16000.00까지의 이미지 파일이 있지만 일반적인 범위는 훨씬 적을 수 있습니다. 내가하고 싶은 것은이 값을 정수 범위 0-100으로 압축하는 것입니다. 여기서 0은 가장 작은 지점의 값이고 100은 가장 큰 값입니다. 약간의 정밀도가 손실 되더라도 사이의 모든 지점은 상대 비율을 유지해야합니다. 파이썬 에서이 작업을 수행하고 싶지만 일반적인 알고리즘도 충분해야합니다. 최소/최대 범위를 조정할 수있는 알고리즘을 선호합니다 (즉, 두 번째 범위는 0 ~ 100 대신 -50 ~ 800이 될 수 있습니다).
해결책
NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
또는 좀 더 읽기 쉬운 :
OldRange = (OldMax - OldMin)
NewRange = (NewMax - NewMin)
NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
또는 기존 범위가 0 인 경우를 보호하려면 (Oldmin = Oldmax):
OldRange = (OldMax - OldMin)
if (OldRange == 0)
NewValue = NewMin
else
{
NewRange = (NewMax - NewMin)
NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
}
이 경우 가능한 새로운 범위 값 중 하나를 임의로 선택해야합니다. 맥락에 따라 현명한 선택은 다음과 같습니다. NewMin
(샘플을 참조하십시오), NewMax
또는 (NewMin + NewMax) / 2
다른 팁
그것은 단순한 선형 변환입니다.
new_value = ( (old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min
따라서 -16000 ~ 16000의 스케일로 10000을 0 ~ 100 수율로 변환합니다.
old_value = 10000
old_min = -16000
old_max = 16000
new_min = 0
new_max = 100
new_value = ( ( 10000 - -16000 ) / (16000 - -16000) ) * (100 - 0) + 0
= 81.25
실제로 위의 답변이 깨질 경우가 있습니다. 잘못된 입력 값, 잘못된 입력 범위, 음의 입력/출력 범위와 같은.
def remap( x, oMin, oMax, nMin, nMax ):
#range check
if oMin == oMax:
print "Warning: Zero input range"
return None
if nMin == nMax:
print "Warning: Zero output range"
return None
#check reversed input range
reverseInput = False
oldMin = min( oMin, oMax )
oldMax = max( oMin, oMax )
if not oldMin == oMin:
reverseInput = True
#check reversed output range
reverseOutput = False
newMin = min( nMin, nMax )
newMax = max( nMin, nMax )
if not newMin == nMin :
reverseOutput = True
portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
if reverseInput:
portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin)
result = portion + newMin
if reverseOutput:
result = newMax - portion
return result
#test cases
print remap( 25.0, 0.0, 100.0, 1.0, -1.0 ), "==", 0.5
print remap( 25.0, 100.0, -100.0, -1.0, 1.0 ), "==", -0.25
print remap( -125.0, -100.0, -200.0, 1.0, -1.0 ), "==", 0.5
print remap( -125.0, -200.0, -100.0, -1.0, 1.0 ), "==", 0.5
#even when value is out of bound
print remap( -20.0, 0.0, 100.0, 0.0, 1.0 ), "==", -0.2
확인중인 모든 값이 동일 할 때 @jerryjvl의 코드가 NAN을 반환하는 조건이 있습니다.
if (OldMin != OldMax && NewMin != NewMax):
return (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
else:
return (NewMax + NewMin) / 2
나는 그것을 파지 않았다 BNF 이를 위해서는 Arduino 문서에는 기능의 훌륭한 예가 있으며 고장이되었습니다. 나는 단순히 레프 리밍을 다시 만들기 위해 파이썬에서 사용할 수 있었고 (원인 맵은 내장 된 원인) 유형 캐스트와 곱슬 브레이스를 제거합니다 (즉, 모든 'long's를 제거).
원래의
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
파이썬
def remap(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
Penguintd가 제공 한 목록에서, 나는 왜 범위가 반전되는지 이해하지 못하고, 범위를 되돌릴 필요없이 작동합니다. 선형 범위 변환은 선형 방정식을 기반으로합니다 Y=Xm+n
, 어디 m
그리고 n
주어진 범위에서 파생됩니다. 범위를 다음과 같이 지칭하기보다는 min
그리고 max
, 그것들을 1과 2로 지칭하는 것이 좋습니다. 따라서 공식은 다음과 같습니다.
Y = (((X - x1) * (y2 - y1)) / (x2 - x1)) + y1
어디에 Y=y1
언제 X=x1
, 그리고 Y=y2
언제 X=x2
. x1
, x2
, y1
& y2
주어질 수 있습니다 positive
또는 negative
값. 매크로에서 표현식을 정의하면 더 유용하게 만들어지면 인수 이름과 함께 사용할 수 있습니다.
#define RangeConv(X, x1, x2, y1, y2) (((float)((X - x1) * (y2 - y1)) / (x2 - x1)) + y1)
그만큼 float
캐스트는 모든 인수가있는 경우 부동 소수점을 보장합니다 integer
가치. 응용 프로그램에 따라 범위를 확인할 필요가 없습니다. x1=x2
그리고 y1==y2
.
PHP 포트
Penguintd의 솔루션이 도움이 되었기 때문에 PHP에 포팅했습니다. 알아서 드세요!
/**
* =====================================
* Remap Range
* =====================================
* - Convert one range to another. (including value)
*
* @param int $intValue The value in the old range you wish to convert
* @param int $oMin The minimum of the old range
* @param int $oMax The maximum of the old range
* @param int $nMin The minimum of the new range
* @param int $nMax The maximum of the new range
*
* @return float $fResult The old value converted to the new range
*/
function remapRange($intValue, $oMin, $oMax, $nMin, $nMax) {
// Range check
if ($oMin == $oMax) {
echo 'Warning: Zero input range';
return false;
}
if ($nMin == $nMax) {
echo 'Warning: Zero output range';
return false;
}
// Check reversed input range
$bReverseInput = false;
$intOldMin = min($oMin, $oMax);
$intOldMax = max($oMin, $oMax);
if ($intOldMin != $oMin) {
$bReverseInput = true;
}
// Check reversed output range
$bReverseOutput = false;
$intNewMin = min($nMin, $nMax);
$intNewMax = max($nMin, $nMax);
if ($intNewMin != $nMin) {
$bReverseOutput = true;
}
$fRatio = ($intValue - $intOldMin) * ($intNewMax - $intNewMin) / ($intOldMax - $intOldMin);
if ($bReverseInput) {
$fRatio = ($intOldMax - $intValue) * ($intNewMax - $intNewMin) / ($intOldMax - $intOldMin);
}
$fResult = $fRatio + $intNewMin;
if ($bReverseOutput) {
$fResult = $intNewMax - $fRatio;
}
return $fResult;
}
JS에서 해결하는 문제 에이 솔루션을 사용 했으므로 번역을 공유 할 것이라고 생각했습니다. 설명과 해결책에 감사드립니다.
function remap( x, oMin, oMax, nMin, nMax ){
//range check
if (oMin == oMax){
console.log("Warning: Zero input range");
return None;
};
if (nMin == nMax){
console.log("Warning: Zero output range");
return None
}
//check reversed input range
var reverseInput = false;
oldMin = Math.min( oMin, oMax );
oldMax = Math.max( oMin, oMax );
if (oldMin != oMin){
reverseInput = true;
}
//check reversed output range
var reverseOutput = false;
newMin = Math.min( nMin, nMax )
newMax = Math.max( nMin, nMax )
if (newMin != nMin){
reverseOutput = true;
};
var portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
if (reverseInput){
portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin);
};
var result = portion + newMin
if (reverseOutput){
result = newMax - portion;
}
return result;
}
C ++ 변형
Penguintd의 솔루션이 유용한 것을 발견 했으므로 누군가가 필요한 경우 C ++에 포팅했습니다.
float remap (float x, float omin, float omax, float nmin, float nmax) {
//range check if( oMin == oMax) { //std::cout<< "Warning: Zero input range"; return -1; } if( nMin == nMax){ //std::cout<<"Warning: Zero output range"; return -1; } //check reversed input range bool reverseInput = false; float oldMin = min( oMin, oMax ); float oldMax = max( oMin, oMax ); if (oldMin == oMin) reverseInput = true; //check reversed output range bool reverseOutput = false; float newMin = min( nMin, nMax ); float newMax = max( nMin, nMax ); if (newMin == nMin) reverseOutput = true; float portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin); if (reverseInput) portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin); float result = portion + newMin; if (reverseOutput) result = newMax - portion; return result; }
다음은 전체 목록을 확장 할 수있는 기능을 포함하여 복사 및 붙여 넣기를위한 짧은 파이썬 기능입니다.
def scale_number(unscaled, to_min, to_max, from_min, from_max):
return (to_max-to_min)*(unscaled-from_min)/(from_max-from_min)+to_min
def scale_list(l, to_min, to_max):
return [scale_number(i, to_min, to_max, min(l), max(l)) for i in l]
그렇게 사용할 수 있습니다.
scale_list([1,3,4,5], 0, 100)
[0.0, 50.0, 75.0, 100.0]
제 경우에는 로그 곡선을 확장하고 싶었습니다.
scale_list([math.log(i+1) for i in range(5)], 0, 50)
[0.0, 21.533827903669653, 34.130309724299266, 43.06765580733931, 50.0]
바로 가기/단순화 된 제안
NewRange/OldRange = Handy multiplicand or HM
Convert OldValue in OldRange to NewValue in NewRange =
(OldValue - OldMin x HM) + NewMin
웨인
저는 제네릭을 지원하는 도우미 클래스를 개인적으로 사용합니다 (Swift 3 호환)
struct Rescale<Type : BinaryFloatingPoint> {
typealias RescaleDomain = (lowerBound: Type, upperBound: Type)
var fromDomain: RescaleDomain
var toDomain: RescaleDomain
init(from: RescaleDomain, to: RescaleDomain) {
self.fromDomain = from
self.toDomain = to
}
func interpolate(_ x: Type ) -> Type {
return self.toDomain.lowerBound * (1 - x) + self.toDomain.upperBound * x;
}
func uninterpolate(_ x: Type) -> Type {
let b = (self.fromDomain.upperBound - self.fromDomain.lowerBound) != 0 ? self.fromDomain.upperBound - self.fromDomain.lowerBound : 1 / self.fromDomain.upperBound;
return (x - self.fromDomain.lowerBound) / b
}
func rescale(_ x: Type ) -> Type {
return interpolate( uninterpolate(x) )
}
}
이 예제는 노래를 현재 위치를 20-40의 각도 범위로 변환합니다.
/// <summary>
/// This test converts Current songtime to an angle in a range.
/// </summary>
[Fact]
public void ConvertRangeTests()
{
//Convert a songs time to an angle of a range 20 - 40
var result = ConvertAndGetCurrentValueOfRange(
TimeSpan.Zero, TimeSpan.FromMinutes(5.4),
20, 40,
2.7
);
Assert.True(result == 30);
}
/// <summary>
/// Gets the current value from the mixValue maxValue range.
/// </summary>
/// <param name="startTime">Start of the song</param>
/// <param name="duration"></param>
/// <param name="minValue"></param>
/// <param name="maxValue"></param>
/// <param name="value">Current time</param>
/// <returns></returns>
public double ConvertAndGetCurrentValueOfRange(
TimeSpan startTime,
TimeSpan duration,
double minValue,
double maxValue,
double value)
{
var timeRange = duration - startTime;
var newRange = maxValue - minValue;
var ratio = newRange / timeRange.TotalMinutes;
var newValue = value * ratio;
var currentValue= newValue + minValue;
return currentValue;
}
다음은 미리 정해진 소스 및 대상 범위에 대한 저장을 수행하는 함수를 반환하여 매번 수행 해야하는 계산량을 최소화하는 JavaScript 버전입니다.
// This function returns a function bound to the
// min/max source & target ranges given.
// oMin, oMax = source
// nMin, nMax = dest.
function makeRangeMapper(oMin, oMax, nMin, nMax ){
//range check
if (oMin == oMax){
console.log("Warning: Zero input range");
return undefined;
};
if (nMin == nMax){
console.log("Warning: Zero output range");
return undefined
}
//check reversed input range
var reverseInput = false;
let oldMin = Math.min( oMin, oMax );
let oldMax = Math.max( oMin, oMax );
if (oldMin != oMin){
reverseInput = true;
}
//check reversed output range
var reverseOutput = false;
let newMin = Math.min( nMin, nMax )
let newMax = Math.max( nMin, nMax )
if (newMin != nMin){
reverseOutput = true;
}
// Hot-rod the most common case.
if (!reverseInput && !reverseOutput) {
let dNew = newMax-newMin;
let dOld = oldMax-oldMin;
return (x)=>{
return ((x-oldMin)* dNew / dOld) + newMin;
}
}
return (x)=>{
let portion;
if (reverseInput){
portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin);
} else {
portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
}
let result;
if (reverseOutput){
result = newMax - portion;
} else {
result = portion + newMin;
}
return result;
}
}
다음은이 기능을 사용하여 0-1로 -0x80000000, 0x7ffffff로 확장하는 예입니다.
let normTo32Fn = makeRangeMapper(0, 1, -0x80000000, 0x7FFFFFFF);
let fs = normTo32Fn(0.5);
let fs2 = normTo32Fn(0);
이해력 하나의 라이너 솔루션을 나열하십시오
color_array_new = [int((((x - min(node_sizes)) * 99) / (max(node_sizes) - min(node_sizes))) + 1) for x in node_sizes]
더 긴 버전
def colour_specter(waste_amount):
color_array = []
OldRange = max(waste_amount) - min(waste_amount)
NewRange = 99
for number_value in waste_amount:
NewValue = int((((number_value - min(waste_amount)) * NewRange) / OldRange) + 1)
color_array.append(NewValue)
print(color_array)
return color_array