I want to find the position of the repeated elements in the column "1" and the column "2" in a specified matrix "x" (the matrix "x" has a nx3 size ("n" is the maximum number between the column "1" and the column "2", in the next example is max(3,4)=4)), for then in the principal diagonal (number 1 to "n") of a new matrix "z"(the matrix "z" has a "n"x"n" size) sum the values for each column (except the columns with values equal to zero), and put the respective values off-diagonal in the respective column. This is an example of a matrix "x":
1 2 3
x= 1 [[1,2,4],
2 [1,3,2],
3 [2,3,1],
4 [3,4,5]]
The matrix "z" is a symmetric matrix, meaning for example the element z[1,2] is equal to z[2,1] and the element z[1,3] is equal to z[3,1].This is the structure of the matrix "z":
z= [[z[1],z[1 to 2],z[1 to 3],z[1 to 4]],
[z[2 to 1],z[2],z[2 to 3],z[2 to 4]],
[[z[3 to 1],z[3 to 2],z[3],z[3 to 4]],
[[z[4 to 1],z[4 to 2],z[4 to 3],z[4]]
The elements in the diagonal principal( for this example are):
z[1]=z[1 to 2]+z[1 to 3]+z[1 to 4]
z[2]=z[2 to 1]+z[2 to 3]+z[2 to 4]
z[3]=z[3 to 1]+z[3 to 2]+z[3 to 4]
z[4]=z[4 to 1]+z[4 to 2]+z[4 to 3]
The desired matrix (for this example) is:
z= [[4+2=6,4,2,0],
[4,4+1=5,1,0],
[2,1,2+1+5=8,5],
[0,0,5,5]]
Note: The zeros in the matrix "z" represent if there aren´t connection with the other numbers. In this example the number "4" has only connection with the number "3"(the value is 5). Then the connection for the number 4 with the number 1 and number 2 are zero.
Thanks.
Your problem is clarified if you introduce graph-theoretic terminology. Your x
could be thought of as describing a weighted graph. A row like [1,3,2]
can be interpreted as describing the existence of an edge of weight 2 connecting node 1 and node 3. The matrix z
contains the following information:
1) For each node i
, the corresponding diagonal entry is the sum of the weights of all edges which are incident with i
.
2) For each pair of distinct nodes i
and j
, the corresponding entry of z
is either 0
if the nodes are not connected, or the weight of the edge connecting i
and j
if they are (or sum of the weights if there are multiple edges).
Given this interpretation, things are easy enough. The following assumes that the nodes in x
begin with 1
. Note that the resulting matrix is 0-based even though the graph is 1-based, so to find the weight from node 2 to node 3 you will need z[1][2]
:
def makeMatrix(edges):
n = max(max(edge[:2]) for edge in edges)
z = [[0]*n for i in range(n)]
for edge in edges:
i,j,w = edge
z[i-1][i-1] += w
z[j-1][j-1] += w
z[i-1][j-1] += w
z[j-1][i-1] += w
return z
The following helps for debugging:
def pprint(matrix):
for row in matrix:
print(' '.join(str(i) for i in row))
For example,
>>> x = [[1,2,4],[1,3,2],[2,3,1],[3,4,5]]
>>> z = makeMatrix(x)
>>> pprint(z)
6 4 2 0
4 5 1 0
2 1 8 5
0 0 5 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.