根据无向网络生成了一个邻接矩阵,想解决的问题是在非0处随机赋值。比如矩阵中的非0数有10个,想要7个位置赋值[1,3]范围,另外3个位置赋值在[4,10]范围。我只想到了如何在非0处随机赋值。
import networkx as nx import matplotlib.pyplot as plt import numpy as np import random #无向图 BA=nx.barabasi_albert_graph(5,1)#barabasi_albert_graph(n, m)方法生成一个含有n个节点、每次加入m条边的BA无标度网络 n = nx.number_of_nodes(BA) m = nx.number_of_edges(BA) # print(n,m) matrix = nx.to_numpy_matrix(BA) matrix_up = np.triu(matrix)#取矩阵的上三角 num = 0 for i in range(n): for j in range(n): if matrix_up[i][j] != 0: num = num +1 matrix_up[i][j] = random.randint(1,9) print('改变后的邻接矩阵:\n',matrix_up)
哪位大佬能帮忙解答下嘛,炒鸡炒鸡感谢!!!