R
RobG
I was playing with matrices and wrote a routine to return the minor
matrix if given a matrix and index. The minor is the matrix given by
removing the first row and i-th column.
e.g. using JavaScript indices and a 3x3 matrix:
if A = | 1 2 3 |
| 4 5 6 |
| 7 8 9 |
then minor(A,0) = | 5 6 |
| 8 9 |
minor(A,1) = | 4 6 |
| 7 9 |
minor(A,2) = | 4 5 |
| 7 8 |
Here's my routine to do it:
// A is a square matrix
// i is the minor index
function minor(A, i) {
A.shift()
var j = A.length;
while (j--) {
A[j].splice(i,1);
}
return A;
}
But when I pass it a matrix (object with arrays) the original object is
modified as well. Kinda sux because the alternative is to copy the
elements across individually. What's going on?
Here's a test case that shows ob3 before being passed to the routine,
what is returned and then the state of ob3 afterward. The minor matrix
is correct, but the original ob3 has been modified.
<input type="button" value="minor test" onclick="
var ob3 = [];
ob3[0] = [ 1, 2, 3 ];
ob3[1] = [ 4, 5, 6 ];
ob3[2] = [ 7, 8, 9 ];
alert('ob3 before:\n' + ob3.join('\n'));
alert('minor(0): \n' + minor(ob3, 0).join('\n'));
alert('ob3 after: \n' + ob3.join('\n'));
">
<script type="text/javascript">
function minor(A, i) {
A.shift()
var j = A.length;
while (j--) {
A[j].splice(i,1);
}
return A;
}
</script>
matrix if given a matrix and index. The minor is the matrix given by
removing the first row and i-th column.
e.g. using JavaScript indices and a 3x3 matrix:
if A = | 1 2 3 |
| 4 5 6 |
| 7 8 9 |
then minor(A,0) = | 5 6 |
| 8 9 |
minor(A,1) = | 4 6 |
| 7 9 |
minor(A,2) = | 4 5 |
| 7 8 |
Here's my routine to do it:
// A is a square matrix
// i is the minor index
function minor(A, i) {
A.shift()
var j = A.length;
while (j--) {
A[j].splice(i,1);
}
return A;
}
But when I pass it a matrix (object with arrays) the original object is
modified as well. Kinda sux because the alternative is to copy the
elements across individually. What's going on?
Here's a test case that shows ob3 before being passed to the routine,
what is returned and then the state of ob3 afterward. The minor matrix
is correct, but the original ob3 has been modified.
<input type="button" value="minor test" onclick="
var ob3 = [];
ob3[0] = [ 1, 2, 3 ];
ob3[1] = [ 4, 5, 6 ];
ob3[2] = [ 7, 8, 9 ];
alert('ob3 before:\n' + ob3.join('\n'));
alert('minor(0): \n' + minor(ob3, 0).join('\n'));
alert('ob3 after: \n' + ob3.join('\n'));
">
<script type="text/javascript">
function minor(A, i) {
A.shift()
var j = A.length;
while (j--) {
A[j].splice(i,1);
}
return A;
}
</script>