function [means,props,timerecs,idx,a] = findk(parent,a,b,kmax,repeats)
data=parent.score;
means=ones(kmax,repeats);
props=ones(kmax,repeats);
timerecs=zeros(kmax,repeats);
idx=zeros(kmax,repeats,size(data(1:100:end,(a:b)),1));
disp('Entering k-means evaluation loop...')
for n=1:repeats
   datasamp=data(1:100:end,(a:b));
   for k=2:kmax
   tic;
   idx(k,n,:)=kmeans(datasamp,k,'Maxiter',200,'emptyaction','drop','replicates',5,'Distance','cityblock');
   sil=silhouette(datasamp,reshape(idx(k,n,:),1,[]));
   means(k,n)=mean(sil);
   props(k,n)=((size(sil(sil>0),1)/size(sil,1)));
   timerecs(k,n)=toc;
   end
end
disp(sprintf('Loop elapsed %0.3f seconds total',sum(sum(timerecs))))
disp('Calculating k-index self-correlation...')
tic;
for k=1:kmax
a(k)=(sum(sum(corr(reshape(idx(k,:,:),repeats,[])))));
end
a(isnan(a))=0;
disp(sprintf('Calculation elapsed %0.3f seconds total',toc))
disp('Rendering plots...')
figure; subplot(3,1,1), plot((a)./((size(idx,2)*size(idx,3)).*((std(means'.*props'))+(0.01*max((std(means'.*props'))))))); xlabel('k'); ylabel('Norm. Self-Correlation /au'); xlim([1 kmax]);
subplot(3,1,2), plot((std(means'.*props'))); xlabel('k'); ylabel('StD Silh. /au'); xlim([1 kmax]);
subplot(3,1,3), plot((mean(means'.*props'))); xlabel('k'); ylabel('Mean Silh. /au'); xlim([1 kmax]); ylim([0 inf]);
disp('Complete.')
notify; %play gong noise when complete.
end