replace random data with dataset

I have this code and I´ve been trying to use a dataset I have instead random numbers, but everytime I try it messes with the code.
k = 5;
numP = 100;
xMax = 50;
yMax = 50;
data = load('data.txt');
xP = data(:, 1);
yP = data(:, 2);
%xP = xMax * rand(1,numP);
%yP = yMax * rand(1,numP);
points = [xP; yP];
[cluster, centr] = kmeans(k, points);
Heres kmeans.m
function [ cluster, centr ] = kmeans( k, P )
numP = size(P,2);
dimP = size(P,1);
randIdx = randperm(numP,k);
centr = P(:,randIdx);
cluster = zeros(1,numP);
clusterPrev = cluster;
iterations = 0;
stop = false;
while stop == false
for idxP = 1:numP
dist = zeros(1,k);
for idxC=1:k
dist(idxC) = norm(P(:,idxP)-centr(:,idxC));
end
[~, clusterP] = min(dist);
cluster(idxP) = clusterP;
end
centr = zeros(dimP,k);
for idxC = 1:k
centr(:,idxC) = mean(P(:,cluster==idxC),2);
end
if clusterPrev==cluster
stop = true;
end
clusterPrev = cluster;
iterations = iterations + 1;
end
end

2 Comments

Image Analyst
Image Analyst on 10 Apr 2019
Edited: Image Analyst on 10 Apr 2019
Are you sure it's running your code? There is a built-in kmeans() function if you have the Statistics and Machine Learning Toolbox. Call your function something different, like pablokmeans().
Also explain how your script "messes with the code". I don't see it opening your m-file and doing anything with it so how does the script mess up the code?
Also attach 'data.txt' with the paper clip icon.
Oh, sorry, the fuction kmeans was me translating it, while in spanish, it didnt call the built in function.
Well, it messes with the code when I change it to read xp and yp from a file dataset. The code wasnt made for it and I cant figure a way to change kmeans.m for it to process the dataset instead of random made numbers. The dataset looks like this:
4,10;
5,3;
10,1;
2,2;
9,2;
6,7;
.
.
.

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 16 Apr 2019
My best guess at your error is that your randomly generated data consists of two [1 x numP] row vectors, but when you read your data in from file, they are two [numP x 1] column vectors. So the algorithm thinks there are only two observations with numP dimensions.

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Asked:

on 10 Apr 2019

Answered:

on 16 Apr 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!