matlab代码计算ci值
时间: 2023-12-28 18:05:00 浏览: 32
Ci值是网络中节点的聚类系数,可以用以下Matlab代码计算:
```matlab
function [C, Cn] = clustering_coef_bu(A)
%CLUSTERING_COEF_BU Clustering coefficient
%
% C = clustering_coef_bu(A);
% [C,CN] = clustering_coef_bu(A);
%
% The clustering coefficient is the fraction of triangles around a node
% (equiv. the fraction of node's neighbors that are neighbors of each other).
%
% Input: A, binary undirected or directed connection matrix
% (note: will be treated as undirected)
%
% Output: C, clustering coefficient vector
% Cn, average clustering coefficient
%
% Notes:
% The clustering coefficient measures the connectedness of nodes and
% is defined here as the fraction of all possible triangles in the
% graph that actually exist. Note that triangles are weighted equally
% here, whereas in some formulations of the clustering coefficient,
% triangles on edges that are part of larger cliques are weighted more
% strongly.
%
% Reference: Watts and Strogatz (1998) Nature 393:440-442.
%
% Mika Rubinov, UNSW/U-Cambridge, 2007-2012
% Modification History:
% Jul 2007: Original (contributed by Michael Breakspear)
% Mar 2008: Addressed some minor issues and improved documentation.
% (Thanks to Cosimo Lacava)
% May 2008: Output Cn added.
% May 2012: Addressed some minor issues in the documentation.
% (Thanks to Noam Peled)
% ensure binary and symmetrical
A = double(A~=0);
A = A + A.';
N = size(A,1);
% number of triangles = trace(A^3)/2
A3 = A^3;
triangles = trace(A3)/2;
% number of connected triples = K*(K-1)/2
K = sum(A,1).';
if isempty(K)
warning('Empty graph: clustering coefficients undefined.');
C = NaN(N,1);
Cn = NaN;
return
end
connected_triplets = sum(K.*(K-1))/2;
% calculate clustering coefficient
if triangles==0 % according to definition, when no triangles exist, C=0
C = zeros(N,1);
else
C = triangles./connected_triplets;
end
% calculate average clustering coefficient
Cn = nanmean(C);
```
使用方法:
1. 输入一个二进制的无向连接矩阵A。
2. 返回节点的聚类系数C,以及平均聚类系数Cn。
注意:该函数仅适用于无向图。如果你有一个有向图,你可以使用`clustering_coef_wd`函数。