function [CC_global] = clustering_coefficient_v2(conn_edges,dim,neuron)
%CLUSTERING COEFFICIENT       computes the clustering coefficient as a
% ratio of triangles/triplets.
%

%   Input:      conn_edges,    a matrix of 2 columns where each row
%               represents the connection between the neuron of the first 
%               column and the second one.
%                                                         
%   Output:     CC_global,     the global connected component

triangles = 0;
triplets_a  = 0;
triplets_b  = [];
aux = 1;
for i = 1:size(conn_edges,1)
    A = conn_edges(i,1);
    B = conn_edges(i,2);
    for j = 1:dim
        C = neuron(j);
        if C ~= A && C ~= B
            edgesA = [conn_edges(conn_edges(:,1)==A,2);conn_edges(conn_edges(:,2)==A,1)];
            edgesB = [conn_edges(conn_edges(:,1)==B,2);conn_edges(conn_edges(:,2)==B,1)];
            if ismember(C,edgesA) && ismember(C,edgesB)
                triangles = triangles + 1;
                triplets_a  = triplets_a + 1;
            elseif ismember(C,edgesA) || ismember(C,edgesB)
                triplets_b(aux,1) = A;
                triplets_b(aux,2) = B;
                triplets_b(aux,3) = C;
                aux = aux+1;
            end
        end
    end
end

%find the doubled counted triplets in triplets_b
triplets_b = sort(triplets_b');
triplets_b = unique(triplets_b','rows');

triplets   = triplets_a + length(triplets_b);
    

CC_global = triangles/triplets;

end