1D->二维阵列W /法线曲线子阵列长度 [英] 1D -> 2D Array W/Normal Curve Sub-Array Lengths

查看:99
本文介绍了1D->二维阵列W /法线曲线子阵列长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将1D数组分解为2D数组,其中子数组的长度不同。这种变化应遵循高斯曲线(或丘形)。因此,假设我们制作的2D数组变量名为gaussianCurve。 gaussianCurve [0]&中的数组gaussianCurve [n]的长度为1,而gaussianCurve [n / 2]的长度为参数 maxArrayLength提供的最大值。这迫使gaussianCurve索引的数量变为变量。

I am trying to break a 1D array into a 2D array where the sub-arrays are of varying lengths. This variance should follow the gaussian curve [or a mound shape]. So, say the 2D array variable we make is named gaussianCurve. The array within gaussianCurve[0] & gaussianCurve[n] would be of length 1, and gaussianCurve[n/2] would be a maximum provided by a parameter "maxArrayLength". This forces the number of gaussianCurve indexes to become variable.

说我有以下伪代码:

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
}

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

Why would I want such a thing? Progress bars.


  1. 他们没有显示我正在立即取得进步

  1. They don’t show I am making progress immediately

  1. 这是因为必须先完成第一项工作,酒吧才能移动


  • 它们放慢速度达到95%以上,有时甚至会保持100%

  • They slow down at 95%+ and sometimes even stick at 100%


    1. 只是烦人


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

    Any suggestions are welcome. I am just not seeing the answer in my minds eye.

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

    I feel it was worded poorly, so I am rewording it.

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

    ...gaussianCurve[0].length & gaussianCurve[gaussianCurve.length - 1].length would be 1, and gaussianCurve[gaussianCurve.length/2].length would be up to "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"]]
    

    N内部数组可能超出maxArrayLength的长度

    No inner array may exceed the length of maxArrayLength

    推荐答案

    我给了它一个快速镜头,它似乎可以正常工作。一些潜在的改进:

    I gave it a quick shot and it seems to work. Some potential improvements:


    • 输入检查功能

    • 它将所有可能的剩余值放入中间垃圾箱。对于偶数总数的箱,它将受益于一些平衡。之后,尝试根据输入数据中的原始索引对每个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' ] ]
    */

    这篇关于1D-&gt;二维阵列W /法线曲线子阵列长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆