replace random data with dataset

1 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2019-4-10
编辑:Image Analyst 2019-4-10
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.
Pablo Villarreal
Pablo Villarreal 2019-4-16
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;
.
.
.

请先登录,再进行评论。

回答(1 个)

the cyclist
the cyclist 2019-4-16
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.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by