Could somebody please help me to create an array of array.I have a matrix calculator and I want to create an array of arrays ('smaller').How can I do it?The functiions create2Darray and calculateDet are working ok,so now problem with them.Would be really grateful for the help.I need an array of these arrays (main matrix's minors) to calculate their determinants and calculate the inverted matrix Here is the HTML:
有人能帮我创建一个数组吗?我有一个矩阵计算器,我想创建一个数组的数组('小')。我该怎么做呢?函数create2Darray和calculateDet工作正常,所以现在问题就来了。非常感谢你的帮助。我需要这些数组的数组(主矩阵的余子)来计算它们的行列式,然后计算倒矩阵,这里是HTML:
<div id = "table4">
<div class = "calcHeader">Macierz odwrotna [3x3]</div>
<form id = "row1"><!--first row-->
<input type = "text" class = "det3"/>
<input type = "text" class = "det3"/>
<input type = "text" class = "det3"/>
</form>
<form id = "row2"><!--second row-->
<input type = "text" class = "det3"/>
<input type = "text" class = "det3"/>
<input type = "text" class = "det3"/>
</form>
<form id = "row3"><!--third row-->
<input type = "text" class = "det3"/>
<input type = "text" class = "det3"/>
<input type = "text" class = "det3"/>
</form>
<div class = "count" onclick="invertMat('det3')"><a href = "#">Wylicz</a></div>
</div>
Javascript
Javascript
function invertMat(matrixClass) {
var matrix = create2Darray(matrixClass);
var det = calculateDet(matrix);
//alert(det);
var invMat = new Array();
for (var i = 0; i < matrix.length; i++) {
invMat[i] = new Array();
for (var j = 0; j < matrix.length; j++) {
invMat[i][j] = 0;
}
}
if (matrix.length == 2) {
invMat[0][0] = 1 / det * matrix[1][1];
invMat[1][0] = 1 / det * (-matrix[1][0]);
invMat[0][1] = 1 / det * (-matrix[0][1]);
invMat[1][1] = 1 / det * matrix[0][0];
alert(invMat);
} else if (matrix.length == 3) {
//var smaller = new Array();//creating an empty array for a matrix minor
for (var i = 0; i < matrix.length; i++) {
var smaller = new Array(matrix.length - 1);
for (h = 0; h < smaller.length; h++) {
smaller[h] = new Array(matrix.length - 1);
}
for (a = 1; a < matrix.length; a++) {
for (b = 0; b < matrix.length; b++) {
if (b < i) {
smaller[a - 1][b] = matrix[a][b];
} else if (b > i) {
smaller[a - 1][b - 1] = matrix[a][b];
}
}
}
}
}
}
2 个解决方案
#1
1
Try Like this Fiddle
试着这样的小提琴
var array1 = new Array();
array1.push(1);
array1.push(2);
var array2 = new Array();
array2.push(3);
array2.push(4);
var array3 = new Array();
array3.push(array1);
array3.push(array2);
console.log(array3[0]);
console.log(array3[1]);
#2
2
现场演示
function create2Array(d1, d2, fn) {
var arr = [],
d = function(x, y) {},
f = fn || d;
for (var i = 0; i < d1; i++) {
for (var j = 0, curr = []; j < d2; j++) {
curr[j] = f.call(window, i, j);
};
arr[i] = curr;
};
return arr;
};
function print2DArray(arr) {
document.body.innerHTML += "<p><b>Array:</b></p>";
for (var i = 0, len = arr.length; i< len; i++) {
document.body.innerHTML += "<p><b>" + i + "</b>: " + arr[i].join(" ") + "</p>";
};
};
var matrix = create2Array(10, 10, function(x, y) { return 0;}),
m2 = create2Array(10, 10, function(x, y) { return x + y;});
print2DArray(matrix);
print2DArray(m2);
Use it like this:
使用它是这样的:
var matrix = create2Array(10, 10);
Or you can even specify a custom init function which takes the index as param. Say if you want to init your matrix with 0 by default:
或者您甚至可以指定一个自定义的init函数,该函数将索引作为param。如果你想在默认情况下用0 init你的矩阵:
var matrix = create2Array(10, 10, function(x, y) { return 0;});
#1
1
Try Like this Fiddle
试着这样的小提琴
var array1 = new Array();
array1.push(1);
array1.push(2);
var array2 = new Array();
array2.push(3);
array2.push(4);
var array3 = new Array();
array3.push(array1);
array3.push(array2);
console.log(array3[0]);
console.log(array3[1]);
#2
2
现场演示
function create2Array(d1, d2, fn) {
var arr = [],
d = function(x, y) {},
f = fn || d;
for (var i = 0; i < d1; i++) {
for (var j = 0, curr = []; j < d2; j++) {
curr[j] = f.call(window, i, j);
};
arr[i] = curr;
};
return arr;
};
function print2DArray(arr) {
document.body.innerHTML += "<p><b>Array:</b></p>";
for (var i = 0, len = arr.length; i< len; i++) {
document.body.innerHTML += "<p><b>" + i + "</b>: " + arr[i].join(" ") + "</p>";
};
};
var matrix = create2Array(10, 10, function(x, y) { return 0;}),
m2 = create2Array(10, 10, function(x, y) { return x + y;});
print2DArray(matrix);
print2DArray(m2);
Use it like this:
使用它是这样的:
var matrix = create2Array(10, 10);
Or you can even specify a custom init function which takes the index as param. Say if you want to init your matrix with 0 by default:
或者您甚至可以指定一个自定义的init函数,该函数将索引作为param。如果你想在默认情况下用0 init你的矩阵:
var matrix = create2Array(10, 10, function(x, y) { return 0;});