function connection_matrix = generateConnectionMatrix(nbNode, nbGroup, probaIntraGroup, probaInterGroup)
% 
% GENERATECONNECTIONMATRIX: create the connections between the nodes
% 
% CONNECTION_MATRIX: return a symmetric matrix with the connections betwwen
% nodes. 1 at a_ij means a connection between node i and node j. 0 means no
% connection.
%   [nbNode, nbNode]

% Check the input
assert(nbNode >= 1);
assert(nbGroup >= 1);
assert(mod(nbNode, nbGroup) == 0); % We want the same number of nodes in each group
assert(probaIntraGroup >= 0);
assert(probaIntraGroup <= 1);
assert(probaInterGroup >= 0);
assert(probaInterGroup <= 1);

nbNodePerGroup = nbNode / nbGroup;

% Connection matrix: a_ij = 1 for a connection between node i and j, 0
% otherwise. The matrix is symmetric, with the diagonal equal to 0.

% Initialize
% Create a matrix with each entry having a probability of probaInterGroup
connection_matrix = rand(nbNode, nbNode);
connection_matrix = (connection_matrix <= probaInterGroup);

for g=1:nbGroup
    
    % Create a matrix with each entry having a probability of probaIntraGroup
    intra = rand([nbNodePerGroup, nbNodePerGroup]);
    intra = (intra <= probaIntraGroup); %
    
    % If we have only one group, there is no need for inter group
    % probability, and we can exit.
    if (nbGroup == 1)
        connection_matrix = intra;
        return;
    end
    
    % We fill the intra group values    
    start = (g-1)*nbNodePerGroup+1;
    stop = round(start + nbNodePerGroup - 1);
    connection_matrix(start:stop, start:stop) = intra;
    
  
end

% We make the matrix symmetric and make the diagonal 0
connection_matrix = triu(connection_matrix, 1) + triu(connection_matrix, 1)';  

for i=1:nbNode
   line = connection_matrix(i, :);
   if (nnz(line) == 0)
      j = randi([1, nbNode], 1, 1); 
      connection_matrix(i, j) = 1;
      connection_matrix(j, i) = 1;
   end
end





end