I have half of an N x N matrix in a javascript array of arrays, and i need to get this half and "mirror" it on the other side of the main diagonal.
Here is an image that explains better:
The main diagonal is the red line, and i have the upper half of the matrix that needs to be "placed" below the red line too, forming a full matrix.
The data structure is something like this:
var map = [ ["0","1","2","3"], ["0", "1", "2"], ["0","1"], ["0"] ];
Lines with columns inside.
I often insert things i already tried in my questions, but in this time i couldn't even find out how to start. So, my apologize if i am not providing some more info.
One way to do it is something like this
var i = 0;
var j = 0;
var map = [ ["0","1","2","3"], ["0", "1", "2"], ["0","1"], ["0"] ];
var n = map.length;
var res = new Array(n);
for (i = 0; i < n; i++) {
res[i] = new Array(n);
for (j = 0; j < n - i; j++) {
res[i][i+j] = map[i][j];
}
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
res[j][i] = res[i][j];
}
}
res
will contain the mirrored array