findSigClu.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. function [vecSigCl,vecSigClI,nSigCl,sSigCl] = findSigClu(vecCl,hCl)
  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. % convert cluster vector into cluster index vextor
  7. vecClI = vecCl;
  8. vecClI(vecCl~=0) = 1;
  9. % count number of all clusters in vector
  10. vecClIs = [0,vecClI]; % add 0 at start to make sure possible cluster starting at point 1 is considered
  11. vecClIe = [vecClI,0]; % add 0 at the end to make sure possible cluster lasting until the end is considered
  12. startCl = find(diff(vecClIs)==1); % find all points where difference vector is 1 (starting points of clusters in original vector)
  13. endCl = find(diff(vecClIe)==-1); % find all points where difference vector is -1 (ending points of clusters in original vector)
  14. nCl = numel(startCl); % return number of cluster starting points = number of clusters
  15. % replace non-significant clusters by 0s
  16. vecSigClI = vecClI;
  17. for cl = 1:nCl
  18. if hCl(cl)==0
  19. vecSigClI(startCl(cl):endCl(cl)) = 0;
  20. end
  21. end
  22. % create vector that contains only significant clusters
  23. vecSigCl = vecCl.*vecSigClI;
  24. % count number of significant clusters in vector
  25. vecSigClIs = [0,vecSigClI]; % add 0 at start to make sure possible cluster starting at point 1 is considered
  26. vecSigClIe = [vecSigClI,0]; % add 0 at the end to make sure possible cluster lasting until the end is considered
  27. startSigCl = find(diff(vecSigClIs)==1); % find all points where difference vector is 1 (starting points of clusters in original vector)
  28. endSigCl = find(diff(vecSigClIe)==-1); % find all points where difference vector is -1 (ending points of clusters in original vector)
  29. nSigCl = numel(startSigCl); % return number of cluster starting points = number of clusters
  30. % calculate size of each cluster
  31. sSigCl = zeros(nSigCl,1);
  32. for c = 1:nSigCl
  33. sSigCl(c) = sum(vecSigCl(startSigCl(c):endSigCl(c))); % calulate size of each identified cluster
  34. end
  35. end