agreement.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. function D = agreement(ci,buffsz)
  2. %AGREEMENT Agreement matrix from clusters
  3. %
  4. % D = AGREEMENT(CI) takes as input a set of vertex partitions CI of
  5. % dimensions [vertex x partition]. Each column in CI contains the
  6. % assignments of each vertex to a class/community/module. This function
  7. % aggregates the partitions in CI into a square [vertex x vertex]
  8. % agreement matrix D, whose elements indicate the number of times any two
  9. % vertices were assigned to the same class.
  10. %
  11. % In the case that the number of nodes and partitions in CI is large
  12. % (greater than ~1000 nodes or greater than ~1000 partitions), the script
  13. % can be made faster by computing D in pieces. The optional input BUFFSZ
  14. % determines the size of each piece. Trial and error has found that
  15. % BUFFSZ ~ 150 works well.
  16. %
  17. % Inputs, CI, set of (possibly) degenerate partitions
  18. % BUFFSZ, optional second argument to set buffer size
  19. %
  20. % Outputs: D, agreement matrix
  21. %
  22. % Richard Betzel, Indiana University, 2012
  23. %modification history
  24. %09.24.2012 - added loop for big N that makes the function slower but also
  25. % prevents it from maxing out memory.
  26. n = size(ci,2);
  27. if nargin < 2
  28. buffsz = 1000;
  29. end
  30. if n <= buffsz
  31. ind = dummyvar(ci);
  32. D = ind*ind';
  33. else
  34. a = 1:buffsz:n;
  35. b = buffsz:buffsz:n;
  36. if length(a) ~= length(b)
  37. b = [b, n];
  38. end
  39. x = [a' b'];
  40. nbuff = size(x,1);
  41. D = zeros(size(ci,1));
  42. for i = 1:nbuff
  43. y = ci(:,x(i,1):x(i,2));
  44. ind = dummyvar(y);
  45. D = D + ind*ind';
  46. end
  47. end
  48. D = D.*~eye(length(D));