从一组数组生成矩阵

时间:2014-12-05 11:58:26

标签: javascript algorithm math



var set1 = [14, 9, 1, 6, 16],
  set2 = [4, 15, 16, 14, 11],
  set3 = [16, 10, 2, 3, 8],
  set4 = [3, 17, 16, 6, 14],
  set5 = [19, 18, 14, 6, 20],
  set6 = [6, 15, 8, 7, 2],
  set7 = [15, 14, 2, 19, 3],
  set8 = [8, 2, 14, 10, 5],
  set9 = [11, 6, 8, 10, 18],
  set10 = [14, 10, 12, 4, 18],
  input = [set1, set2, set3, set4, set5, set6, set7, set8, set9, set10];

// Sort function
function sortFunction(a) {

  var len = a.length,
    temp, i, j;

  for (i = 0; i < len; i++) {
    for (j = i + 1; j < len; j++) {
      if (a[j] < a[i]) {
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
      }
    }
  }
  return a;
}

// Sorting each sets and finding range of each sets
for (var i = 0; i < len; i++) {

  input[i] = sortFunction(input[i]);
  minRange.push(Math.min.apply(null, input[i]));
  maxRange.push(Math.max.apply(null, input[i]));
}

// Finding the range of input

var minimum = Math.min.apply(null, minRange);
var maximum = Math.max.apply(null, maxRange);
var range = maximum - minimum + 1;


// Matrix table function
var tableArray = [];

function table() {
  for (var i = 0; i < len; i++) {
    for (var j = 0; j < range; j++) {
      if (input[i][j] == j) {
        tableArray[i].push(input[i][j]);
      } else {
        tableArray[i].push(0);
      }
    }
    tableArray.push(tableArray[i]);
  }
  return tableArray;
}
&#13;
&#13;
&#13;

我在解决这个问题时遇到了问题:输入是一组10个数组,其中每个数组包含5个不同的数字,范围为1 - 20.

输入= [[14,19,1,6,16],[4,15,16,14,11],[16,10,2,3,8],[3,17,16,6] ,14],[19,18,14,6,20],[6,15,8,7,2],[15,14,2,19,3],[8,2,14,10,5] ],[11,6,8,10,18],[14,1,10,12,4,18]]

我想生成一个10x20矩阵作为输出,其中每行有20个数字,具有以下模式:

output = [[1,0,0,0,0,6,0,0,9,0,0,0,0,14,0,16,0,0,0,0],[0 ,0,0,4,0,0,0,0,0,11,0,0,14,15,16,0,0,0,0],[...],...]

我使用JavaScript来解决这个问题......

3 个答案:

答案 0 :(得分:1)

创建一个新的输出数组:

var out = [];

循环输入数组。对于每个嵌套数组,在输出数组中创建一个用20个零填充的新数组,然后将输出数组中的元素替换为嵌套输入数组中右侧位置的元素值。既然您知道嵌套数组的大小,并且它很小,则只需列出每个元素而不是使用内部循环。

for (var i = 0, l = arr.length; i < l; i++) {
  var el = arr[i];
  out[i] = Uint8Array(20);
  out[i][el[0] - 1] = el[0];
  out[i][el[1] - 1] = el[1];
  out[i][el[2] - 1] = el[2];
  out[i][el[3] - 1] = el[3];
  out[i][el[4] - 1] = el[4];
}

DEMO

如果您的浏览器不支持Uint8Array,您可以使用单独的功能来创建填充数组:

function padArray() {
  var out = [];
  for (var i = 0, l = 20; i < l; i++) {
    out.push(0);
  }
  return out;
}

并使用:

out[i] = padArray();

答案 1 :(得分:0)

你真的应该亲自尝试过。这很容易。从20个零的数组开始,然后使用数组中的值填充插槽:

function posArray(arr, max) {
    var res = [];

    for (var i = 0; i < max; i++) res.push(0);

    for (var i = 0; i < arr.length; i++) {
        var a = arr[i];

        if (a > 0 && a <= max) res[a - 1] = a;
    }

    return res;
}

var output = [];

for (var i = 0; i < input.length; i++) {
    output.push(posArray(input[i], 20));
}

答案 2 :(得分:0)

这样的事情也可行(未经测试):

var set1 = [14, 9, 1, 6, 16],
  set2 = [4, 15, 16, 14, 11],
  set3 = [16, 10, 2, 3, 8],
  set4 = [3, 17, 16, 6, 14],
  set5 = [19, 18, 14, 6, 20],
  set6 = [6, 15, 8, 7, 2],
  set7 = [15, 14, 2, 19, 3],
  set8 = [8, 2, 14, 10, 5],
  set9 = [11, 6, 8, 10, 18],
  set10 = [14, 10, 12, 4, 18],
  input = [set1, set2, set3, set4, set5, set6, set7, set8, set9, set10];
var output = [];

for (var e=0; e<input.length; e++) {
  newRow = [];
  for (var i=0;i<20; i++) {
    if (input[e].indexOf(i) > -1) {
      newRow.push(i);
    }
    else {
      newRow.push(0);
    }
  }
  output.push(newRow);
}
alert(output);