replace random data with dataset

조회 수: 1 (최근 30일)
Pablo Villarreal
Pablo Villarreal 2019년 4월 10일
답변: the cyclist 2019년 4월 16일
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