I want to get all the diagonals of a matrix array using javascript. Supposing the input and output as follows:
input = [
[1,2,3],
[4,5,6],
[7,8,9],
]
output = [
[1],
[4,2],
[7,5,3],
[8,6],
[9],
]
How can I turn the input into the output? How can I do this so that it will work for any size square grid (2x2, 3x3, 4x4, etc.)?
Thanks!
I want to get all the diagonals of a matrix array using javascript. Supposing the input and output as follows:
input = [
[1,2,3],
[4,5,6],
[7,8,9],
]
output = [
[1],
[4,2],
[7,5,3],
[8,6],
[9],
]
How can I turn the input into the output? How can I do this so that it will work for any size square grid (2x2, 3x3, 4x4, etc.)?
Thanks!
Share Improve this question asked Feb 12, 2016 at 23:26 Michael MahonyMichael Mahony 1,3503 gold badges16 silver badges33 bronze badges 5- And how would the triangle output from a 4x4 matrix look like? – L4zl0w Commented Feb 12, 2016 at 23:37
- input = [ [1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] ] – Michael Mahony Commented Feb 12, 2016 at 23:39
- The output won't be a perfect triangle... – L4zl0w Commented Feb 12, 2016 at 23:41
- @L4zl0w - Who said anything about triangles? OP wants diagonals. (Though the output from a 4x4 would be a triangle-shaped array.) – nnnnnn Commented Feb 12, 2016 at 23:45
- The code should output the diagonals as in the example output in the question. – Michael Mahony Commented Feb 13, 2016 at 0:17
3 Answers
Reset to default 2const arr = [
[11, 2, 4],
[4, 5, 6],
[10, 8, 12]
];
function diagonalDifference(arr) {
const primaryDiagonal = arr
.map((e, i) => e[i])
.reduce((mem, curr) => mem + curr, 0);
const secondaryDiagonal = arr
.map((e, i) => {
let index = arr.length - i -1;
return e[index];
})
.reduce((mem, curr) => mem + curr, 0);
return Math.abs(primaryDiagonal - secondaryDiagonal);
}
console.log(diagonalDifference(arr));
The first way that came to mind (not necessarily the best way) is just to have a loop that runs down the left edge of the square followed by a loop that runs along the bottom edge of the square. In other words, I've written code that does exactly what I would do if getting the diagonals by hand, with no clever optimisations.
Note that this should work for any size square, but doesn't try to cope with rectangles: I leave that as an exercise for the reader.
function getDiagonals(m) {
var s, x, y, d,
o = [];
for (s = 0; s < m.length; s++) {
d = [];
for(y = s, x = 0; y >= 0; y--, x++)
d.push(m[y][x]);
o.push(d);
}
for (s = 1; s < m[0].length; s++) {
d = [];
for(y = m.length - 1, x = s; x < m[0].length; y--, x++)
d.push(m[y][x]);
o.push(d);
}
return o;
}
var output = getDiagonals(input);
I gave it a few brief tests and it seems to work, but you can try for yourself with this demo.
You can use this:
var output = new Array(2*input.length-1);
for(var i=0; i<output.length; ++i) {
output[i] = [];
if(i < input.length) for(var j=0; j<=i; ++j)
output[i].push(input[i-j][j]);
else for(var j=input.length-1; j>i-input.length; --j)
output[i].push(input[j][i-j]);
}
Or this:
var output = new Array(2*input.length-1);
for(var i=0; i<output.length; ++i) {
output[i] = [];
for(var j=Math.min(i, input.length-1); j>Math.max(-1, i-input.length); --j)
output[i].push(input[j][i-j]);
}