简体   繁体   中英

Creation of new matrix from A containing the average value of A rows for each column if B[i, j] == 1 where B is an 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

How can we get a new matrix containing the average value of A row for each column if B[i,j] == 1

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

To find the neighborhood if each i, I implemented the following code:

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

Are you sure the second column of your expected output matrix is correct? I feel like this might be what you are looking for:

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

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM