如何将非线性范围指向线性范围并返回?
我有一个代表一个大范围的线性范围列表:
X'
100 200 300 400 500 600 700 | 900 (X)
|----------|----------|----------|--------+----------|
0 | 100 (Y)
Y'
X 由以下范围组成(偶数和整数只是易于理解的示例,它们可以是任何东西,这里根本没有比例) :
- 从 100 到 200
- 从 300 到 400
- 从 500 到 600
- 从 700 到 900
另一方面,Y 只有一个范围:
- 从 0 到 100
X 和 Y 的长度相同,只是单位不同。 假设一个是美元,另一个是百分比(或任何其他类似的不相关单位)。 所以 Y'0 == X'100 且 Y'100 == X'900。
给定 Y 中的任意点,X 中的等价点是什么,反之亦然,给定 X 中的点 - Y 中的等价点是什么?
这是一个典型的数学问题吗? 它有名字吗?
I have a list of linear ranges which represent one big range:
X'
100 200 300 400 500 600 700 | 900 (X)
|----------|----------|----------|--------+----------|
0 | 100 (Y)
Y'
X consists of the following ranges (even and round numbers are just examples for ease of comprehension, they could be anything, no proportions here at all):
- From 100 to 200
- From 300 to 400
- From 500 to 600
- From 700 to 900
On the flip side, Y has just one range:
- From 0 to 100
Both X and Y are of the same length, just different units. Let's say one is dollars and another is percents (or any other similarly unrelated units). So Y'0 == X'100 and Y'100 == X'900.
Given any point in Y, what is equivalent point in X and vise-versa, given a point in X - what is it in Y?
Is this a typical math problem? Does it have a name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你有多少个范围? 该算法是 O(范围数)可以接受吗?
如果是这样,下面是该算法的描述。 让我用你的(原始)例子来解释一下。
1)您要做的是将范围 A (100-800) 中的值 X 映射到连续范围 B (0-399) 中的值 Y(因为范围中的元素总数为 400)。 然后很容易将 B 中的位置更改为百分比,我将省略这部分。
2) 创建一个记录列表,其中每条记录代表一个范围映射。
在您的情况下,您将得到以下列表:
3) 当您需要将数字 X 从 A 映射到 B 时,您迭代列表以查找 start_in_a <= X 的第一条记录。那么您的值 Y 是
4) 算法是对称的,您只需迭代列表即可找到 start_in_b <= Y 的第一条记录,然后
注释 1。出于错误检查目的,您也可以将范围大小保留在 RangeRecord 中。
注 2. 如果 O(范围数) 不够好,请将记录保留为树而不是列表。 然后,您将需要 O(log(范围数)) 运算,
How many ranges do you have? Is it acceptable that the algorithm is O(number of ranges)?
If so, below is the description of the algorithm. Let me explain it on your (original) example.
1) What you're doing to do is to map the value X in range A (100-800) to the value Y in continous range B (0-399) (as the total number of elements in your range is 400). Then it's easy to change position in B to percents, I will omit this part.
2) Create a list of records, where each records represents one range mapping.
In your case, you will get the following list:
3) When you need to map a number X from A to B, you iterate the list to find first record with start_in_a <= X.Then your value Y is
4) The algorithm is symmettric, you just iterate the list to find the first record with start_in_b <= Y, and then
Note 1. For error checking purposes, you might keep the range size in RangeRecord, as well.
Note 2. If O(number of ranges) is not good enough, keep the records as a tree instead of a list. You will need O(log(number of ranges)) operations then,
假设您有一个范围 (a, b) 和另一个范围 (c, d)。 现在你有一个数字 i ,其中 a < 我< b. 您可以通过减去 a 并除以 b - a 对其进行“标准化” - 这会得到一个 0 到 1 之间的值。然后您可以使用该值通过与其他边界反转此计算来将其转移到其他范围,以便说乘以 (d - c) 再加上 c。
假设另一个范围内的对应点是 i'。 然后,
您正在搜索的术语是缩放和平移。
Say you have one range (a, b) and another one (c, d). Now you have a number i for which a < i < b. You can "normalize" it by subtracting a and dividing by b - a - this gives you a value between 0 and 1. You can then use this value to transfer it into the other range by reversing this calculation with the other bounds, so to speak multiply it by (d - c) and add c.
Say the corresponding point in the other range is i'. Then,
The term you are searching for is scaling and translation.
这并不是真正可以解决的,因为问题没有明确说明。 即使对于相同的范围,也可以有不同的滑块,如下所示:
对于像
[1..100]
这样的每个范围,您需要知道滑块上的哪些百分比点如何与其对应。 在上面的示例中,这可能类似于[0%..33%]
或[0%..66%]
。 一旦掌握了这些信息,就可以轻松确定给定数据点位于哪个范围、该范围的哪个位置以及它对应的值。This is not really solvable because the problem is underspecified. Even for the same ranges, there can be different sliders like this:
For each range like
[1..100]
you need to know how which percent points on the slider correspond to it. In the above examples this could be something like[0%..33%]
or[0%..66%]
. Once you have this information, it's easy to determine in which of the ranges and at which position of that range a given data point is and to what value it corresponds.从某些 X' 转换为 Y' 时,您需要调整三件事,反之亦然:
考虑类似的范围 Z 可能会有所帮助(至少在开发解决方案时),该范围为 0 到 503,并且与 X 中的 504 个可能值具有一对一映射。也就是说,对于每个不连续性,如果 X 值大于不连续点的上端,则减去 99(不连续点的大小)。 那么X'100 = Z'0,X'200 = Z'100,X'300 = Z'101,X'400 = Z'201,X'500 = Z'202等。Z范围的引入解决了上面列表中的问题 1 和 2。
要从 Z 转换为 Y,只需乘以 101/504,即可将 Z 缩放到 Y。
You have three things you need to adjust for in converting from some X' to Y' and vice versa:
It might be helpful (at least while developing your solutions) to consider a similar range Z, which is the range 0 to 503 and has a one-to-one mapping with the 504 possible values in X. That is, for each discontinuity, if the X value is greater than the upper end of the discontinuity, subtract 99 (the size of the discontinuity). Then X'100 = Z'0, X'200 = Z'100, X'300 = Z'101, X'400 = Z'201, X'500 = Z'202, etc. The introduction of the Z range resolves problems 1 and 2 in the list above.
To convert from Z to Y, you just multiply by 101/504, which scales Z onto Y.
假设您暗示的分段线性排列,您可以通过以下方式找到 X:
以及 Y 的相反值:
编辑:此解决方案适用于您给出的原始范围:
当然,反向公式仅适用于 X 的有效值。
这是一个图的两条曲线。 绿色是您的原始规格,蓝色是反向曲线(同样,仅对有效的 x 值有效)。
替代文本 http://img523.imageshack.us/img523/8858/66945008.png< /a>
Assuming the piecewise linear arrangement you imply, you can find X by:
and the reverse for Y:
edit: This solution works for the original range you gave:
And of course, the reverse formula only holds for valid values of X.
Here's a figure of the two curves. The green is your original specification and the blue is the reverse curve (again, only valid for the valid x-values).
alt text http://img523.imageshack.us/img523/8858/66945008.png