插入“缺失年份”在 JavaScript 中?

发布于 2024-11-14 23:33:12 字数 553 浏览 5 评论 0原文

之前:

id   year  value
SE   1950  67
SE   1960  71
SE   1965  82
NO   1975  65
NO   1985  75

之后:

data : {
    SE : {
        data : {
             1950 : 67,
             1951 : 67.4,
             1952 : 67.8,
             [...]
             1965 : 82
        },
        min_year : 1950,
        max_year : 1965

    }    
    NO : {
        data : {
             [...]
        },
        [...]   
    }    
} 

那么基本上,根据 JS 中的相邻值填充间隙/插值的最有效方法是什么?

Before:

id   year  value
SE   1950  67
SE   1960  71
SE   1965  82
NO   1975  65
NO   1985  75

After:

data : {
    SE : {
        data : {
             1950 : 67,
             1951 : 67.4,
             1952 : 67.8,
             [...]
             1965 : 82
        },
        min_year : 1950,
        max_year : 1965

    }    
    NO : {
        data : {
             [...]
        },
        [...]   
    }    
} 

So basically, what is the most effective way of filling the gaps/interpolating based on adjacent values in JS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲性难收 2024-11-21 23:33:12

JS 作为一种语言没有任何工具可以直接帮助你。

您可以非常轻松地实现线性或多项式插值。多项式(比如 3 次)可能会在中间提供稍微更好的数字,尽管端点可能有问题 - 取决于数据。

线性插值更容易,尽管总的来说,我认为它不会提供像更高次多项式插值那样准确的估计。

另一种选择是样条线(三次相对容易),它对于您的目的来说足够准确。这可能有点矫枉过正,尽管也许不是——不确定这个要求的范围。

您可能会考虑在服务器端执行此操作,并使用实现这些插值函数的其他语言的许多库之一。这将为您提供一种真正准确且通用的方法来有效地解决问题,而无需您自己实现。

这很难说,因为准确度、数据范围和项目范围都会影响您需要什么类型的插值、是否可以在服务器/客户端完成等。

JS as a language has no tools to help you directly.

You can implement linear or polynomial interpolation quite easily. Polynomial (of degree say 3) will probably provide slightly nicer numbers in the middle, although the endpoints can be problematic - depends on the data.

Linear interpolation is easier although on the whole I would presume it wouldn't give as accurate an estimation as a higher degree polynomial interpolation.

An alternative could be splines (cubic are relatively easy) which will be more than accurate enough for your purposes. It might be slight overkill, although maybe not - not sure the scope of this requirement.

You might consider doing this server-side and using one of many libraries for other languages that implement these interpolatory functions. That would give you a really accurate and general way to solve the problem efficiently without having to implement it yourself.

It's difficult to say since the degree of accuracy, the range of data and the scope of the project all factor in to what type of interpolation you need, if it can be done server/client side, etc.

知足的幸福 2024-11-21 23:33:12

如果您只想要直线插值,则将 n_0n_1 作为您确实知道其值的索引:

val[n] = val[n_0] + (n - n_0) * (val[n_1] - val[n_0]) / (n_1 - n_0);

给定 SE 数据,例如,1960 年的插值将为:

67.8 + (1960 - 1952) * (82 - 67.8) / (1965 - 1952)

即大约 76.5

If you just want straight linear interpolation, then given n_0 and n_1 as indices for which you do know the values:

val[n] = val[n_0] + (n - n_0) * (val[n_1] - val[n_0]) / (n_1 - n_0);

Given your data for SE, for example, the interpolated value for 1960 would be:

67.8 + (1960 - 1952) * (82 - 67.8) / (1965 - 1952)

i.e. about 76.5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文