replace random data with dataset
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Image Analyst
le 10 Avr 2019
Modifié(e) : Image Analyst
le 10 Avr 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.
Réponses (1)
the cyclist
le 16 Avr 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.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!