MATLAB Answers


replace random data with dataset

Asked by Pablo Villarreal on 10 Apr 2019
Latest activity Answered by the cyclist
on 16 Apr 2019
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));
[~, clusterP] = min(dist);
cluster(idxP) = clusterP;
centr = zeros(dimP,k);
for idxC = 1:k
centr(:,idxC) = mean(P(:,cluster==idxC),2);
if clusterPrev==cluster
stop = true;
clusterPrev = cluster;
iterations = iterations + 1;


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:

Sign in to comment.

1 Answer

Answer by 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.


Sign in to comment.