function [vecCl,vecClI,nCl,sCl] = findClu(hV,tStat,lenMin) % IDENTIFY CLUSTERS IN OBSERVED DATA % This function identifies clusters in the observed data, based on the % given minimum length of consecutive points. It also counts the number % of clusters and returns the size of each identified cluster % create vector that contains all clusters % code from forum: replace ones by number of consecutive repetitions d = [1,diff(hV)~=0,1]; % TRUE if values change n = diff(find(d)); % Number of repetitions X = repelem(n,n); vecClI = X.*hV; vecClI(vecClI=lenMin) = 1; % replace numbers that are equal or larger than criterion (X consecutive repetitions or more) by 1 % index into t-value vector to extract clusters (based on % defined criterion) vecCl = tStat.*vecClI; % count number of clusters in vector vecClIs = [0,vecClI]; % add 0 at start to make sure possible cluster starting at point 1 is considered vecClIe = [vecClI,0]; % add 0 at the end to make sure possible cluster lasting until the end is considered startCl = find(diff(vecClIs)==1); % find all points where difference vector is 1 (starting points of clusters in original vector) endCl = find(diff(vecClIe)==-1); % find all points where difference vector is -1 (ending points of clusters in original vector) nCl = numel(startCl); % return number of cluster starting points = number of clusters % calculate size of each cluster sCl = zeros(nCl,1); for c = 1:nCl sCl(c) = sum(vecCl(startCl(c):endCl(c))); % calulate size of each identified cluster end end