如果B [i,j] == 1,则从A创建新矩阵,其中包含每列A行的平均值,其中B是邻接矩阵

时间:2020-07-17 12:20:19

标签: python numpy matrix mean adjacency-matrix

Suppose we have a matrix 

A =  1    2    3    4
     15   20   7   10 
     0    5   18   12


And an adjacency matrix 


B =   1     0     1    
      0     0     1    
      1     1     1

如果B [i,j] == 1,我们如何获得包含每一行A行平均值的新矩阵

Expected output matrix 
C =  0.5     3.5     10.5     8
     7.5    12.5     12.5     11
     5.33     9      9.33    8.66

要查找每个i的邻域,我实现了以下代码:

for i in range(A.shape[0]):
    for j in range(A.shape[0]):
        if (B[i,j] == 1):
            print(j)```

1 个答案:

答案 0 :(得分:1)

您确定预期输出矩阵的第二列正确吗? 我觉得这可能就是您要寻找的东西

import numpy as np

A = np.array([[1,2,3,4], [15,20,7,10], [0,5,18,12]])
B = np.array([[1,0,1], [0,0,1], [1,1,1]])

np.divide(np.dot(A.T,B), B.sum(axis=1)).T

enter image description here

相关问题