在Javascript中镜像N x N邻接矩阵的一半(Mirror half of a N x N adjacency matrix in Javascript)

我在数组的javascript数组中有一半的N x N矩阵,我需要得到这一半并在主对角线的另一侧“镜像”它。

这是一个更好地解释的图像:

主对角线是红线,我有矩阵的上半部分,需要“放置”在红线下方,形成一个完整的矩阵。

数据结构是这样的:

var map = [ ["0","1","2","3"], ["0", "1", "2"], ["0","1"], ["0"] ];

内有列的行。

我经常在我的问题中插入已经尝试过的东西,但在这个时候我甚至无法找到如何开始。 所以,如果我不提供更多信息,我会道歉。

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.

最满意答案

一种方法是这样的

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将包含镜像数组

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

更多推荐