簡體   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