function [IDX,D]=customclust(input_2D,col1,col2,centroids);
%[IDX,D]=customclust(scoresmatrix,score1,score2,centroids) where centroids is row-wise [x y threshold]
global nrows;
%setup basic matrices
coords=input_2D(:,[col1,col2]);
rows=size(input_2D,1);
clusters=size(centroids,1);

%generate distances matrix
D=zeros(rows,clusters);
for k=1:clusters
    for r=1:rows
        D(r,k)=sqrt(((centroids(k,1)-coords(r,1))^2)+((centroids(k,2)-coords(r,2))^2));
    end
end

IDX=zeros(rows,1);

%Create thresholded coordinates matrix tcoords by distance and index
for t=1:clusters
    centcheck=ones(rows,1);
    centcheck=centcheck*centroids(t,3);
    Dcheck=D(:,t);
    ccheck=coords(:,1);
    ccheck(Dcheck > centcheck)=NaN;
    idxcheck=isnan(ccheck);
    IDX(idxcheck == 0)=t;
    tcoords(:,1,t)=ccheck;
    ccheck=coords(:,2);
    ccheck(Dcheck > centcheck)=NaN;
    idxcheck=isnan(ccheck);
    IDX(idxcheck == 0)=t;
    tcoords(:,2,t)=ccheck;
end


%plot all thresholded clusters

IDXcolormap=IDX;%(1:1000:end);
colormap(summer)
fakemap=colormap;
fakemap(1,:)=[0 0 0];
% bcoords1=coords(:,1);
% bcoords1=bcoords1(1:50000:end);
% bcoords2=coords(:,2);
% bcoords2=bcoords2(1:50000:end);
% scatter(coords(:,1),coords(:,2),1);
figure
hold on
for p=1:clusters
    tcoords1=tcoords(:,1,p);
    %tcoords1=tcoords1(1:1000:end);
    tcoords1(tcoords1 == 0)=NaN;
    tcoords2=tcoords(:,2,p);
    %tcoords2=tcoords2(1:1000:end);
    tcoords2(tcoords2 == 0)=NaN;
scatter(tcoords1,tcoords2,5,IDXcolormap);
colormap(summer)
scatter(centroids(p,1),centroids(p,2),36,[1 0 0])
xlabel(sprintf('Score %d',col1));
ylabel(sprintf('Score %d',col2));
title('Custom Cluster Plot');
end
hold off

%map thresholded clusters
figure
IDX(IDX==0)=-1;
rIDX=reshape(IDX,nrows,[]);
rIDX=mat2gray(rIDX);
imshow(rIDX);
title('Custom Cluster Map');
colormap(fakemap)
end