findClu.m 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. function [vecCl,vecClI,nCl,sCl] = findClu(hV,tStat,lenMin)
  2. % IDENTIFY CLUSTERS IN OBSERVED DATA
  3. % This function identifies clusters in the observed data, based on the
  4. % given minimum length of consecutive points. It also counts the number
  5. % of clusters and returns the size of each identified cluster
  6. % create vector that contains all clusters
  7. % code from forum: replace ones by number of consecutive repetitions
  8. d = [1,diff(hV)~=0,1]; % TRUE if values change
  9. n = diff(find(d)); % Number of repetitions
  10. X = repelem(n,n);
  11. vecClI = X.*hV;
  12. vecClI(vecClI<lenMin) = 0; % replace numbers that are smaller than criterion (less than X consecutive repetitions) by 0
  13. vecClI(vecClI>=lenMin) = 1; % replace numbers that are equal or larger than criterion (X consecutive repetitions or more) by 1
  14. % index into t-value vector to extract clusters (based on
  15. % defined criterion)
  16. vecCl = tStat.*vecClI;
  17. % count number of clusters in vector
  18. vecClIs = [0,vecClI]; % add 0 at start to make sure possible cluster starting at point 1 is considered
  19. vecClIe = [vecClI,0]; % add 0 at the end to make sure possible cluster lasting until the end is considered
  20. startCl = find(diff(vecClIs)==1); % find all points where difference vector is 1 (starting points of clusters in original vector)
  21. endCl = find(diff(vecClIe)==-1); % find all points where difference vector is -1 (ending points of clusters in original vector)
  22. nCl = numel(startCl); % return number of cluster starting points = number of clusters
  23. % calculate size of each cluster
  24. sCl = zeros(nCl,1);
  25. for c = 1:nCl
  26. sCl(c) = sum(vecCl(startCl(c):endCl(c))); % calulate size of each identified cluster
  27. end
  28. end