繁体   English   中英

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

[英]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

如果 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)```

您确定您预期的 output 矩阵的第二列是正确的吗? 我觉得这可能是您正在寻找的:

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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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