我正在尝试将1D数组分解为2D数组,其中子数组的长度不同。这种变化应遵循高斯曲线(或丘形)。因此,假设我们制作的2D数组变量名为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
}


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


他们没有表明我正在立即取得进步


这是因为必须先完成第一个作业,然后钢筋才能移动

他们放慢速度达到95%以上,有时甚至会坚持100%


只是烦人



欢迎任何建议。我只是在脑海中看不到答案。

编辑:我觉得它的措词很差,所以我改写了它。

... 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 - 一维-> 2D数组WITH/法线曲线子数组长度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55943151/

10-11 05:40