How to color each cluster with different color ?

10 Ansichten (letzte 30 Tage)
sam mohel
sam mohel am 18 Apr. 2022
Bearbeitet: sam mohel am 22 Apr. 2022
function cluster_dp(filename, percent)
shapeset = load(filename);
showShapeSet(shapeset);
distset = shapeset2distset(shapeset);
%dc = computeDc(distset, percent);
dc = 0.048;
fprintf('average percentage of neighbours (hard coded): %5.6f\n', percent);
fprintf('Computing Rho with gaussian kernel of radius: %12.6f\n', dc);
rhos = getLocalDensity(distset, dc);
%rho_delta = load('vc/rho_delta.txt');
%rhos = rho_delta(:, 1)';
[deltas, nneigh] = getDistanceToHigherDensity(distset, rhos);
%deltas = rho_delta(:, 2)';
showDeltas(rhos, deltas);
[min_rho, min_delta] = selectRect();
filter = (rhos > min_rho) & (deltas > min_delta);
cluster_num = sum(filter);
fprintf('rho: %f, delta: %f, number of clusters: %i \n', min_rho, min_delta, cluster_num);
ords = find(filter);
cluster = zeros(size(rhos));
color = 1;
for i = 1:size(ords, 2)
cluster(ords(i)) = color;
color = color + 1;
end
[sorted_rhos, rords] = sort(rhos, 'descend');
for i = 1:size(rords, 2)
if cluster(rords(i)) == 0
neigh_cluster = cluster(nneigh(rords(i)));
assert(neigh_cluster ~= 0, 'neigh_cluster has not assign!');
cluster(rords(i)) = neigh_cluster;
end
end
showColorShape(shapeset, cluster, cluster_num, ords);
halo = cluster;
dc_filter = (distset ~= 0) & (distset < dc);
elnum = size(distset, 1);
rhos_matrix = repmat(rhos', 1, elnum);
rhos_items = repmat(rhos, elnum, 1);
rho_filter = rhos_matrix > rhos_items;
cluster_filter = repmat(cluster', 1, elnum) ~= repmat(cluster, elnum, 1);
halo_filter = dc_filter & rho_filter & cluster_filter;
bord_rhos = zeros(size(halo_filter));
bord_rhos(halo_filter) = (rhos_matrix(halo_filter) + rhos_items(halo_filter)) / 2;
r = max(bord_rhos, [], 1);
c = max(bord_rhos, [], 2);
d = [r; c'];
e = max(d, [], 1);
[row, col, v] = find(e);
cluster_rho = zeros(cluster_num);
for i = col
if e(i) > cluster_rho(cluster(i))
cluster_rho(cluster(i)) = e(i);
end
end
for i = 1:elnum
if rhos(i) < cluster_rho(cluster(i))
halo(i) = 0;
end
end
showElementCount(cluster, halo, cluster_num);
showHaloShape(shapeset, cluster, halo, cluster_num, ords);
end
function showElementCount(cluster, halo, cluster_num)
faa = fopen("assign.txt","w")
for i = 0:cluster_num
halo_filter = (halo == i);
cluster_filter = (cluster == i);
nc = length(find(cluster_filter));
nh = length(find(halo_filter));
fprintf('CLUSTER: %i, ELEMENTS: %i, CORE: %i, HALO: %i\n', i, nc, nh, nc-nh);
fprintf(faa, '%i %i %i\n',i,cl(i),halo(i));
end
end
function [min_rho, min_delta] = selectRect()
subplot(2,2,2);
rect = getrect
%fprintf('rect(x:%i y:%i width:%i height:%i)\n', rectangle(1), rectangle(2), rectangle(3), rectangle(4));
min_rho = rect(1);
min_delta = rect(2);
end
function showHaloShape(shapeset, cluster, halo, cluster_num, ords)
subplot(2,2,4);
hold on;
cmap = colormap;
for i = 0:cluster_num
filter = (halo == i);
x = shapeset(:, 1)';
y = shapeset(:, 2)';
xx = x(filter);
yy = y(filter);
ic = int8(i * 32.0 / cluster_num) + 1;
%fprintf('i: %d, cluster_element: %d\n', i, size(xx, 2));
tt=plot(xx, yy, 'o', 'MarkerSize', 2, 'MarkerFaceColor', cmap(ic,:), 'MarkerEdgeColor', cmap(ic,:));
end
for i = 1:size(ords, 2)
color = cluster(ords(i));
x = shapeset(ords(i), 1);
y = shapeset(ords(i), 2);
ic = int8(color * 64.0 / cluster_num);
tt=plot([x], [y], 'o', 'MarkerSize', 10, 'MarkerFaceColor', cmap(ic,:), 'MarkerEdgeColor', cmap(ic,:));
end
text = strcat('HaloShape: ', num2str(cluster_num));
title (text, 'FontSize', 15.0);
xlabel ('x');
ylabel ('y');
end
function showColorShape(shapeset, cluster, cluster_num, ords)
subplot(2,2,3);
hold on;
cmap = colormap;
color = lines(6); % Generate color values
for i = 0:cluster_num
filter = (cluster == i);
x = shapeset(:, 1)';
y = shapeset(:, 2)';
xx = x(filter);
yy = y(filter);
ic = int8(i * 32.0 / cluster_num) + 1;
%fprintf('i: %d, cluster_element: %d\n', i, size(xx, 2));
tt=plot(xx, yy, 'o', 'MarkerSize', 2, 'MarkerFaceColor', cmap(ic,:), 'MarkerEdgeColor', cmap(ic,:));
%C = hsv(6);
%gscatter(xx,yy,color(1:3,:))
end
for i = 1:size(ords, 2)
color = cluster(ords(i));
x = shapeset(ords(i), 1);
y = shapeset(ords(i), 2);
ic = int8(color * 64.0 / cluster_num);
tt=plot([x], [y], 'o', 'MarkerSize', 10, 'MarkerFaceColor', cmap(ic,:), 'MarkerEdgeColor', cmap(ic,:));
end
text = strcat('ColorShape: ', num2str(cluster_num));
title (text, 'FontSize', 15.0);
xlabel ('x');
ylabel ('y');
end
function showShapeSet(shapeset)
scrsz = get(0,'ScreenSize');
figure('Position', [6 72 scrsz(3)/2. scrsz(4)/1.3]);
subplot(2,2,1);
tt = plot(shapeset(:, 1), shapeset(:, 2), 'o', 'MarkerSize', 2, 'MarkerFaceColor', 'k', 'MarkerEdgeColor', 'k');
text = 'LoadShape';
title (text, 'FontSize', 15.0);
xlabel ('x');
ylabel ('y');
end
function showDeltas(rhos, deltas)
subplot(2,2,2);
tt = plot(rhos(:), deltas(:), 'o', 'MarkerSize', 3, 'MarkerFaceColor', 'k', 'MarkerEdgeColor', 'k');
text = strcat('max rho: ', num2str(max(rhos)), ', delta: ', num2str(max(deltas)));
title (text, 'FontSize', 15.0);
xlabel ('rho');
ylabel ('delta');
end

Antworten (1)

Image Analyst
Image Analyst am 18 Apr. 2022
Very simple. Just use the function made for that. It's called gscatter().
  5 Kommentare
Image Analyst
Image Analyst am 22 Apr. 2022
Attach all needed m-files with the paperclip icon. then tell us which m-file needs to be run to run your code, and if any input arguments need to be assigned in advance.
sam mohel
sam mohel am 22 Apr. 2022
Bearbeitet: sam mohel am 22 Apr. 2022
i attached all the files needed to run the code . you should run main.m file to run the code. inside in this file you will speicify the text file location which contains the points need to be cluster .. Thanks a lot for helping

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by