Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 1 of 1

10142013, 01:34 AM #1
 Join Date
 Oct 2013
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
My kmeans implementation in matlab fails
Hi, I have some homework I need to do, I've been at it all the weekend, but now I have to go to sleep and it's not working ok... I don't know if it's a good place to ask, but any hint would be really appreciated.
Here goes my code:
Code:function [ matrix ] = k_means( matrix, clusters, init_option ) %KMEANS Arranges the data from a matrix into several clusters. % matrix: a parsed, standardized matrix % clusters: the number of groups of data to be formed within the matrix % Get the number of instances of the data matrix [elements,~] = size(matrix); % Get an initial random seed for each cluster of data seeder = get_Seeds ( matrix, clusters, init_option ); % Affix a new attribute 'cluster tag' to each element of the data % matrix. It marks membership to a certain cluster. Initial value = 0 cluster_tag = zeros(elements, 1); matrix = [cluster_tag, matrix]; % Initialize prior distances out of main loop, to control convergence prior_distances = zeros(elements, clusters); distances = zeros(elements, clusters); % Compute membership of each instance of the matrix to a cluster. while 1 % Initialize a container for recalculated centroids new_centroids =[]; % Initialize a matrix to store the distance from each element % to each seed. prior_distances = distances distances = zeros(elements, clusters); % Iterate through each instance of the matrix for i = 1:elements % Iterate through each seed vector for j = 1:clusters seed = seeder(j,:); % Calculate Euclidean distance between a certain instance % of the matrix and each seed, and store distances to % determine the minimum one. one_element = matrix(i,:); dist = one_element(2:end)  seed; dist = dist.^2; dist = sqrt(sum(dist)); distances(i,j) = dist; end % CLUSTERIZATION (by tagging) % Get the index of the minimum distance to identify the seed % that has been used to calculate this distance, and thus assign % it as the cluster to the element  matrix(i,:) [~, index_cluster] = min(distances(i,:)); matrix(i,1) = index_cluster; end % Recalculate centroids according to the current clustering. cluster_container = []; % Check the elements tagged as belonging to a certain cluster % Actually group them into a matrix and calculate their new centroid for j = 1:clusters for i = 1:elements if matrix(i,1) == j one_element = matrix(i,:); cluster_container = [cluster_container; one_element(2:end)]; end end new_centroids = [new_centroids; mean(cluster_container)]; end % Look for stop condition if distances == prior_distances disp('CONVERGED'); % Return sorted matrix with each element tagged with its cluster matrix = sort(matrix); % Return matrix without the cluster tag, just sorted by cluster % (Comment next line to keep the tag column) %matrix = matrix(1:end,2:end); % Exit the algorithm break else %Repeat the clustering proccess with new centroids seeder = new_centroids; end end end