makelatticeCIJ.m 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. function [CIJ] = makelatticeCIJ(N,K)
  2. %MAKELATTICECIJ Synthetic lattice network
  3. %
  4. % CIJ = makelatticeCIJ(N,K);
  5. %
  6. % This function generates a directed lattice network without toroidal
  7. % boundary counditions (i.e. no ring-like "wrapping around").
  8. %
  9. % Inputs: N, number of vertices
  10. % K, number of edges
  11. %
  12. % Outputs: CIJ, connection matrix
  13. %
  14. % Note: The lattice is made by placing connections as close as possible
  15. % to the main diagonal, without wrapping around. No connections are made
  16. % on the main diagonal. In/Outdegree is kept approx. constant at K/N.
  17. %
  18. %
  19. % Olaf Sporns, Indiana University, 2005/2007
  20. % initialize
  21. CIJ = zeros(N);
  22. CIJ1 = ones(N);
  23. KK = 0;
  24. cnt = 0;
  25. seq = 1:N-1;
  26. % fill in
  27. while (KK<K)
  28. cnt = cnt + 1;
  29. dCIJ = triu(CIJ1,seq(cnt))-triu(CIJ1,seq(cnt)+1);
  30. dCIJ = dCIJ+dCIJ';
  31. CIJ = CIJ + dCIJ;
  32. KK = sum(sum(CIJ));
  33. end;
  34. % remove excess connections
  35. overby = KK-K;
  36. if(overby>0)
  37. [i,j] = find(dCIJ);
  38. rp = randperm(length(i));
  39. for ii=1:overby
  40. CIJ(i(rp(ii)),j(rp(ii))) = 0;
  41. end;
  42. end;