Skip to content

Instantly share code, notes, and snippets.

@kmader
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmader/9942606 to your computer and use it in GitHub Desktop.
Save kmader/9942606 to your computer and use it in GitHub Desktop.
Simple layers with weighted k-means
simplelay=importdata('simple_layer.csv',',',1);
disp(simplelay.colheaders)
%% parse the data in matlab
% A tiny piece of code to find which column the given header is in
findColumn=@(idstrut,colName) find(not(cellfun('isempty',strfind(idstrut.colheaders,['"' colName '"']))));
% a function to return all of the data in that column
getColumn=@(idstrut,colName) idstrut.data(:,findColumn(idstrut,colName));
%% show the data
figure(1)
subplot(1,2,1);
plot(getColumn(simplelay,'x'),getColumn(simplelay,'y'),'r.')
title('Point Location')
subplot(1,2,2);
hist(getColumn(simplelay,'Volume'))
pause(0.5)
%% make two groups based on just position
inKvec=[getColumn(simplelay,'x') getColumn(simplelay,'y')];
groupId=kmeans(inKvec,2)
% show the groups as different colors
xdata=getColumn(simplelay,'x');
ydata=getColumn(simplelay,'y');
figure(2)
plot(xdata(groupId==1),ydata(groupId==1),'r.')
hold on;
plot(xdata(groupId==2),ydata(groupId==2),'bo')
legend({'Group 1','Group 2'});
hold off;
%% make two groups based on position and volume
inKvec=[getColumn(simplelay,'x') getColumn(simplelay,'y') getColumn(simplelay,'Volume')];
groupId=kmeans(inKvec,2,'EmptyAction','singleton')
% show the groups as different colors
xdata=getColumn(simplelay,'x');
ydata=getColumn(simplelay,'y');
figure(3)
plot(xdata(groupId==1),ydata(groupId==1),'r.')
hold on;
plot(xdata(groupId==2),ydata(groupId==2),'bo')
legend({'Group 1','Group 2'});
hold off;
%% make two groups based on position and volume normalized
inKvec=normc([getColumn(simplelay,'x') getColumn(simplelay,'y') getColumn(simplelay,'Volume')]);
groupId=kmeans(inKvec,2,'EmptyAction','singleton')
% show the groups as different colors
xdata=getColumn(simplelay,'x');
ydata=getColumn(simplelay,'y');
figure(4)
plot(xdata(groupId==1),ydata(groupId==1),'r.')
hold on;
plot(xdata(groupId==2),ydata(groupId==2),'bo')
legend({'Group 1','Group 2'});
hold off;
%% make two groups based on position and volume normalized
inKvec=[20*getColumn(simplelay,'x') 1/1000*getColumn(simplelay,'y') 1/8.410*(getColumn(simplelay,'Volume')-1000)];
groupId=kmeans(inKvec,2,'EmptyAction','singleton')
% show the groups as different colors
xdata=getColumn(simplelay,'x');
ydata=getColumn(simplelay,'y');
figure(5)
plot(xdata(groupId==1),ydata(groupId==1),'r.')
hold on;
plot(xdata(groupId==2),ydata(groupId==2),'bo')
legend({'Group 1','Group 2'});
hold off;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment