当前位置: 首页 > 工具软件 > bct > 使用案例 >

利用BCT计算一些图论参数

宇文俊明
2023-12-01

clc;clear
%%%%%%%%%%%%%%%%%%%%%%%%%%测试数据导入%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load(['输入的连接矩阵数据' ])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:
% 用途:
% 输入:
%   ():
%   ():
% 输出:
%   ():
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:r = assortativity_bin(CIJ,flag)
% 用途:无权网络同配系数计算
% 输入:
%   (CIJ):无权矩阵
%   (fiag):矩阵类型,0为无向强度,0为无向强度
% 输出:
%   (r)同配系数assortativity coefficient
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[A] = transition_01(FC_1,0.5,1);
assortativity_bin = assortativity_bin(A,0);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:r = assortativity_wei(CIJ,flag)
% 用途:无权网络同配系数计算
% 输入:
%   (CIJ):有权矩阵
%   (fiag):矩阵类型,0为无向强度,0为无向强度
% 输出:
%   (r)同配系数assortativity coefficient
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
assortativity_wei = assortativity_wei(A,0);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:BC = betweenness_bin(G)
% 用途:无权网络的节点介数计算
% 输入:
%   (G):无权矩阵
% 输出:
%   (BC):每个节点的介数betweenness
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[A] = transition_01(FC_1,0.5,1);
betweenness_bin = betweenness_bin(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:BC = betweenness_wei(G)
% 用途:有权网络的节点介数计算
% 输入:
%   (G):有权矩阵
% 输出:
%   (BC):节点的介数betweenness
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
betweenness_wei = betweenness_wei(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:[EBC,BC]=edge_betweenness_bin(G)
% 用途:无权网络的边介数计算
% 输入:
%   (A):无权矩阵
% 输出:
%   (EBC):边的介数betweenness
%   (BC):节点的介数betweenness
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[A] = transition_01(FC_1,0.5,1);
[edge_betweenness_bin1,edge_betweenness_bin2] = edge_betweenness_bin(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:[EBC,BC]=edge_betweenness_wei(G)
% 用途:有权网络的边介数计算
% 输入:
%   (A):有权矩阵
% 输出:
%   (EBC):边的介数betweenness
%   (BC):节点的介数betweenness
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
[edge_betweenness_wei1,edge_betweenness_wei2] = edge_betweenness_wei(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:C=clustering_coef_wd(W)
% 用途:有权有向网络的聚类系数
% 输入:
%   (W):有权有向矩阵
% 输出:
%   (C):聚类系数(cluster coefficient)
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
clustering_coef_wd = clustering_coef_wd(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:C=clustering_coef_wu(W)
% 用途:有权无向网络的聚类系数
% 输入:
%   (W):有权无向矩阵
% 输出:
%   (C):聚类系数(cluster coefficient)
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
clustering_coef_wu = clustering_coef_wu(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:C=clustering_coef_bd(W)
% 用途:无权有向网络的聚类系数
% 输入:
%   (W):无权有向矩阵
% 输出:
%   (C):聚类系数(cluster coefficient)
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[A] = transition_01(FC_1,0.5,1);
clustering_coef_bd = clustering_coef_bd(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:C=clustering_coef_bu(W)
% 用途:无权无向网络的聚类系数
% 输入:
%   (W):无权无向矩阵
% 输出:
%   (C):聚类系数(cluster coefficient)
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[A] = transition_01(FC_1,0.5,1);
clustering_coef_bu = clustering_coef_bu(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:[C_pos,C_neg,Ctot_pos,Ctot_neg] = clustering_coef_wu_sign(W,coef_type)
% 用途:计算有权无向网络的聚类系数(包括3中不同定义)
% 输入:
%   (W):有权无向矩阵
%   (coef_type):聚类系数类型,1代表原始公式,2代表考虑权重的公式,3代表2中公式的推广
% 输出:
%   (C_pos/C_neg):正/负聚类向量,对于3只输出一个向量C_pos
%   (Ctot_pos/Ctot_neg):正/负权的平均聚类系数,对于3只输出Ctot_pos
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
[C_pos,C_neg,Ctot_pos,Ctot_neg] = clustering_coef_wu_sign(A,3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:[lambda,efficiency,ecc,radius,diameter] = charpath(D,diagonal_dist,infinite_dist)
% 几种使用格式:
%   lambda                                  = charpath(D);
%   lambda                                  = charpath(D);
%   [lambda,efficiency]                     = charpath(D);
%   [lambda,efficiency,ecc,radius,diameter] = charpath(D,diagonal_dist,infinite_dist);
% 用途:计算特征路径长度,全局效率及相关统计参数
% 输入:
%   (D):距离矩阵
%   解释:输入距离矩阵可以通过任何距离函数获得,例如distance_bin, distance_wei
%   (diagonal_dist):计算中包括主对角线的距离,默认为0
%   (infinite_dist):在计算中包含无限的距离,默认为1
% 输出:
%   (lambda):网络特征路径长度
%   (efficiency):网络全局效率
%   (ecc):节点离心率
%   (radius):网络半径
%   (diameter):网络直径
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
A = inv(A);
[A,B] = distance_wei(A);  %计算加权网络的最短路径矩阵
charpath = charpath(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:[D,B]=distance_wei(L)
% 几种使用格式:
%   D = distance_wei(L);
%   [D,B] = distance_wei(L);
% 用途:距离矩阵包含所有节点对之间最短路径的长度。
%       其中(u,v)表示从节点u到节点v的最短路径长度,
%       平均最短路径长度是网络的特征路径长度。
% 输入:
%   (L):加权或无权的长度矩阵
% 输出:
%   (D):距离(最短加权路径)矩阵
%   (B):最短加权路径矩阵中的边数
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
A = inv(A);
[distance_wei1,distance_wei2] = distance_wei(A);  %计算加权网络的最短路径矩阵
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:[SPL,hops,Pmat] = distance_wei_floyd(D,transform)
% 用途:计算连接网络中每对节点的最短可能路径的拓扑长度。
% 输入:
%   (D):加权/非加权有向/无向连接*权重* OR *长度*矩阵。
%   (transform):如果输入矩阵是连接*权重*矩阵,则指定将输入连接权重映射到
%   连接长度的转换。有两个转换可用。
%   'log' -> l_ij = -log(w_ij)
%   'inv' -> l_ij = 1/w_ij
%   如果输入矩阵是连接*长度*矩阵,则不指定转换(或指定空转换参数)。
% 输出:
%   (SPL):无加权/加权最短路径长度矩阵。如果是有向矩阵,则SPL是不对称的。
%   (hops):最短路径矩阵中的边数。如果未加权,则SPL和hops相同。
%   (Pmat):这个矩阵的元素[i,j]表示i和j之间最短路径的下一个节点。
%   这个矩阵被用作函数'retrieve_shortest_path的输入参数M ',
%   它返回由给定节点对之间最短路径组成的节点序列作为输出。
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
% A = inv(A);  %可以使用外部求逆得长度矩阵,也可以使用函数中的inv或log
[distance_wei_floyd_SPL,distance_wei_floyd_hops,distance_wei_floyd_Pmat] = distance_wei_floyd(A,'inv');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:[deg] = degrees_und(CIJ)
% 用途:计算网络节点的度
% 输入:
%   (CIJ):无向(无权/加权)连接矩阵
% 输出:
%   (deg):节点度矩阵
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[A] = transition_01(FC_1,0.5,1);
[degrees_und] = degrees_und(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:E = efficiency_wei(W, local)
% 用途:计算有权网络的全局效率和局部效率
% 几种使用格式:
%   Eglob = efficiency_wei(W);      全局
%   Eloc = efficiency_wei(W,2);     局部
% 输入:
%   (W):加权有向或无向连接矩阵
%   (local):0为全局效率(默认),1为局部效率的原始版,2为局部效率的改进版
% 输出:
%   (E):全局/局部效率
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = FC_1;
efficiency_wei = efficiency_wei(A, 2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 函数:[Rw] = rich_club_wu(CIJ,varargin)
% 用途:无向有权网络的富人俱乐部曲线
% 解释:k级的加权富俱乐部系数Rw是连接k级以上节点的边权的比例,这些节点可能共享的最大边权。
% 输入:
%   (CIJ):有权无向连接矩阵
%   (k-level):RC(k)的最大水平(默认k级是CIJ的最大程度)
% 输出:
%   (Rw):富人俱乐部曲线
%%%%%%ex%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[A] = transition_01(FC_1,0.5,1);
[rich_club_wu] = rich_club_wu(A);  %不输入则自动计算到最大,输入level则计算到level
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 

 本文给出了一些利用BCT计算功能连接系数的方法,总结起来自己看,如有纰漏欢迎指出

 类似资料: