gpt4 book ai didi

javascript - 1D -> 2D Array WITH/Normal Curve 子数组长度

转载 作者:行者123 更新时间:2023-11-29 18:42:31 25 4
gpt4 key购买 nike

我试图将一个一维数组分解为一个二维数组,其中子数组的长度各不相同。此方差应遵循高斯曲线 [或丘形状]。因此,假设我们创建的二维数组变量名为 gaussianCurve。 gaussianCurve[0] & gaussianCurve[n] 中的数组长度为 1,而 gaussianCurve[n/2] 将是参数“maxArrayLength”提供的最大值。这迫使 gaussianCurve 索引的数量变得可变。

假设我有以下伪代码:

function (oneDimentionalArray, maxArrayLength) {
// oneDimentionalArray is ["A","B","C","D","E","F","G","H","I","J","K"]
// maxArrayLength is 5
// Currently working like this (i.e. "batches"):
// return [["A","B","C","D","E"],["F","G","H","I","J"],["K"]]
// would LIKE it to work like this
gaussianCurve = []
gaussianCurve.push(["A"])
gaussianCurve.push(["B", "C"])
gaussianCurve.push(["D", "E", "F", "G", "H"])
gaussianCurve.push(["I", "J"])
gaussianCurve.push(["K"])

return gaussianCurve
}

我为什么要这样的东西?进度条。

  1. 它们没有显示我正在立即取得进展
    1. 这是因为第一个工作必须在条可以移动之前完成
  2. 他们在 95% 以上时减速,有时甚至保持在 100%
    1. 只是烦人

欢迎提出任何建议。我只是没有在脑海中看到答案。

编辑:我觉得它措辞不佳,所以我正在重新措辞。

...gaussianCurve[0].length & gaussianCurve[gaussianCurve.length - 1].length 将为 1,而 gaussianCurve[gaussianCurve.length/2].length 将达到“maxArrayLength”。

输入:

function gaussianRefactor(["A","B","C","D","E","F","G","H","I","J","K"], 1)
function gaussianRefactor(["A","B","C","D","E","F","G","H","I","J","K"], 2)
function gaussianRefactor(["A","B","C","D","E","F","G","H","I","J","K"], 4)
function gaussianRefactor(["A","B","C","D","E","F","G","H","I","J","K"], 8)
function gaussianRefactor(["A","B","C","D","E","F","G","H","I","J","K"], 16)

输出:

[["A"],["B"],["C"],["D"],["E"],["F"],["G"],["H"],["I"],["J"],["K"]]
[["A"],["B","C"],["D","E"],["F","G"],["H","I"],["J"],["K"]]
[["A"],["B","C","D"],["E","F","G","H"],["I","J","K"]]
[["A"],["B","C","D","E","F","G","H","I"],["J","K"]]
[["A","B","C","D","E","F","G","H","I","J","K"]]

内部数组的长度不能超过 maxArrayLength

最佳答案

我快速试了一下,它似乎有效。一些潜在的改进:

  • 函数的输入检查
  • 它将所有可能的剩余值放入中间箱。对于偶数总箱,它会受益于一些平衡。之后,尝试根据输入数据中的原始索引对每个 bin 进行排序可能会很好,因为现在事情可能会乱序结束。但是,如果这只是为进度条提供非线性分布的作业,则顺序可能无关紧要。

function probability(s, m, x) {
var eExp = -Math.pow(x - m, 2) /
(2 * Math.pow(s, 2));
return 1/(Math.sqrt(2*Math.PI) * s) *
Math.pow(Math.E, eExp);
}

function gassianArray(input, nBins) {
// first try to determine a reasonable value of s so that the outer bins have a value
var s = 0.1;
var sMax = 10;
var m = (nBins - 1) / 2.0;
var outerBinMinimum = 1 / input.length;
var p = 0;
while (true && s <= sMax) {
p = probability(s, m, 0);
if (p >= outerBinMinimum) {
break;
} else {
s += 0.1;
}
}

// holds arrays
var output = [];
// holds desired array sizes
var outputLengths = [];
// fill these based on probability density
for (var b=0; b<nBins; b++) {
var n = Math.floor(probability(s, m, b) * input.length);
output.push([]);
outputLengths.push(n);
}

// fill arrays from outside, leaving extra values for the middle
var midIndex = Math.floor(m);
// left side
for (var i=0; i<midIndex; i++) {
for (var j=0; j<outputLengths[i]; j++) {
output[i].push(input.shift());
}
}
// right side
for (var i=nBins-1; i>=midIndex; i--) {
for (var j=0; j<outputLengths[i]; j++) {
output[i].push(input.pop());
}
output[i].reverse();
}
// whatever remains goes in the "middle"
while (input.length !== 0) {
output[midIndex].unshift(input.pop());
}

return output;
}

var input = ["A","B","C","D","E","F","G","H","I","J","K"];
var n = 5;
console.log(gassianArray(input, n));
/*
[ [ 'A' ],
[ 'B', 'C' ],
[ 'E', 'D', 'F', 'G', 'H' ],
[ 'I', 'J' ],
[ 'K' ] ]
*/


var input = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var n = 6;
console.log(gassianArray(input, n));
/*
[ [ 'A' ],
[ 'B', 'C', 'D', 'E' ],
[ 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N' ],
[ 'O', 'P', 'Q', 'R', 'S', 'T', 'U' ],
[ 'V', 'W', 'X', 'Y' ],
[ 'Z' ] ]
*/

关于javascript - 1D -> 2D Array WITH/Normal Curve 子数组长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com