How to make this code work? Forel algorithm

1 view (last 30 days)
function [clusters, centers] = forel(X, r)
clusters = {};
centers = [];
X = {2 3 4};
r = [2];
%weights = ones(size(X,2), 1);
x_idx = (1:size(X,1));
i = 0;
while(length(x_idx) > 0)
%find distances to centers of clusters
new_len = 0;
%init new cluster
if (new_len == 0)
i = i + 1;
center = X(x_idx(1), :);
clusters(i) = {x_idx(1)};
x_idx(1) = [];
endif
last_center = center + 1;
%loop update cluster
while (center ~= last_center)
%save last centers
last_center = center;
%dists = w_euclidean_dist(X(x_idx,:), center, weights);
dists = sqrt(sum(bsxfun(@minus, x_idx, center).^2, 2));
x_label = find(dists <= r);
new_len = length(x_label);
%add idxs to the cluster
clusters(i) = [cell2mat(clusters(i)), x_idx(x_label)];
%find new center
center = mean(X(cell2mat(clusters(i)), :));
%remove added elements
x_idx(x_label) = [];
endwhile
%update centers
centers(i,:) = center;
endwhile
end
What should be changed or what values should be specified in the code?

Answers (0)

Categories

Find more on Manage Products in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!