在多维数组中找到两个相关数字的最接近组合

时间:2018-08-09 10:03:05

标签: javascript jquery arrays multidimensional-array nearest-neighbor

我需要根据用户输入来搜索和比较多维数组中两个数字的最接近组合。

此刻,我有类似的东西可以找到最接近的匹配数字,例如“一个数字”。

var count= [10, 20, 30, 40],

  goal = userinput

  var closest = count.reduce(function(prev, curr) {
    return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
  });

$('#result').attr('value', closest);

我要做什么:当用户在 userinput1 4 <<中输入 19 / strong>在 userinput2 中,它应该在多维数组 counts 中搜索并比较数字的最接近组合。

<input id="userinput1 " value="19" type="text">
<input id="userinput2 " value="4" type="text">

-

var userinput1 = $('#userinput1 ').val();
var userinput2 = $('#userinput2 ').val();

counts = [['10', '5'], ['20', '10']];

goal = [[userinput1, userinput2]];

/* NEEDED SCRIPT */

$('#result1').attr('value', closest1);
$('#result2').attr('value', closest2);

在此示例中,应选择['20','10']并在不同的变量中输出两个数字。

HTML结果应为:

<input id="result1" value="20" type="text">
<input id="result2" value="10" type="text">

2 个答案:

答案 0 :(得分:2)

要比较索引方式的绝对绝对值,可以先获取增量,然后根据总和返回数组。

const addAbsDelta = g => (s, v, i) => s + Math.abs(v - g[i]);

var counts = [[10, 5], [20, 10]],
    goal = [19, 4],
    result = counts.reduce((a, b) =>
        a.reduce(addAbsDelta(goal), 0) < b.reduce(addAbsDelta(goal), 0) ? a : b
    );
    
console.log(result);

要获得其中一个值匹配的结果,您需要获得绝对增量与绝对增量之和的乘积,以求结果的顺序,并取最小值。

function getApproximation(counts, goal) {
    const
        p = (r, v, i) => r * Math.abs(v - goal[i]),   // moves zeros to top
        s = (r, v, i) => r + Math.abs(v - goal[i]),   // allowes sorting
        score = a => a.reduce(p, 1) + a.reduce(s, 0); // get product and sum

    return counts
        .filter(a => goal.every((g, i) => a[i] >= g))
        .reduce((a, b) => score(a) <= score(b) ? a : b);
}

var counts = [[5300, 4.2], [6800, 6.0], [5650, 6.6], [2600, 7.0], [4700, 7.0], [3250, 7.3], [3800, 7.5], [3300, 7.8], [9000, 8.2], [5700, 8.5], [7400, 8.5], [6900, 8.7], [4300, 9.0], [5000, 9.5], [6000, 9.5], [7700, 9.5], [2750, 10.0], [5300, 10.0], [6500, 10.0], [8900, 10.5], [6800, 11.0], [3600, 11.4], [4500, 11.5], [9500, 11.5], [5700, 12.0], [5000, 24.0], [6500, 27.0], [7900, 30.0], [5700, 31.0]];

console.log(getApproximation(counts, [2000, 5]));  // [2600, 7]
console.log(getApproximation(counts, [2000, 30])); // [7900, 30]
console.log(getApproximation(counts, [2600, 30])); // [7900, 30] 

答案 1 :(得分:0)

只需添加相应数组元素的差异,然后像以前一样将其最小化即可。

class Mob(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        ...
        ...
var userinput1 = $('#userinput1 ').val();
var userinput2 = $('#userinput2 ').val();

counts = [
  ['10', '5'],
  ['20', '10']
];

goal = [userinput1, userinput2];

let closest = counts[0];
let prevDiff = Math.abs(closest[0] - goal[0]) + Math.abs(closest[1] - goal[1]);
counts.forEach(c => {
  let curDiff = Math.abs(c[0] - goal[0]) + Math.abs(c[1] - goal[1]);
  if (curDiff < prevDiff) {
    closest = c;
    prevDiff = curDiff;
  }
});

$('#result1').val(closest[0]);
$('#result2').val(closest[1]);