{"id":804,"date":"2023-04-08T15:55:40","date_gmt":"2023-04-08T07:55:40","guid":{"rendered":"http:\/\/www.autouav.net.cn\/?p=804"},"modified":"2023-06-15T16:30:06","modified_gmt":"2023-06-15T08:30:06","slug":"matlab-%e7%bb%b4%e8%af%ba%e5%9b%be%e8%b4%a8%e5%bf%83%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.autouav.net.cn\/?p=804","title":{"rendered":"matlab \u7ef4\u8bfa\u56fe\u8d28\u5fc3\u7b97\u6cd5"},"content":{"rendered":"<p>matlab \u7ef4\u8bfa\u56fe\u8d28\u5fc3\u7b97\u6cd5\u3002<\/p>\n<p><code>function [centroids, clusters] = voronoi_centroid(points)<br \/>\n%VORONOI_CENTROID Vornoi diagram centroid-based clustering algorithm<br \/>\n%<br \/>\n%   [CENTROIDS, CLUSTERS] = VORONOI_CENTROID(POINTS) computes the centroids<br \/>\n%   and cluster assignments of a set of n-dimensional points using the<br \/>\n%   Voronoi diagram and the centroids of its regions.<br \/>\n%<br \/>\n%   Arguments:<br \/>\n%       POINTS: A matrix of n-dimensional points, one point per row.<br \/>\n%<br \/>\n%   Returns:<br \/>\n%       CENTROIDS: A matrix with one centroid per row, corresponding to the<br \/>\n%           points in the order they appear in CLUSTERS.<br \/>\n%       CLUSTERS: A vector with one entry per point, indicating the index<br \/>\n%           into the CENTROIDS matrix of the centroid to which that point<br \/>\n%           belongs.<\/code><\/p>\n<p>% Compute the Voronoi diagram.<br \/>\n[~, E] = voronoin(points);<\/p>\n<p>% Initialize the centroids and the clusters.<br \/>\ncentroids = zeros(size(points));<br \/>\nclusters = zeros(length(points), 1);<\/p>\n<p>% Loop over all edges in the Voronoi diagram.<br \/>\nfor i = 1:length(E)<br \/>\n% Indices of the points defining this edge.<br \/>\nidx = E{i};<\/p>\n<p>% If this is an infinite edge, ignore it.<br \/>\nif any(idx == 1), continue; end<\/p>\n<p>% Compute the centroid of the region defined by this edge.<br \/>\nx = mean(points(idx, :));<\/p>\n<p>% Update the closest cluster index for each point in this region.<br \/>\nfor j = idx&#8217;<br \/>\nd = norm(points(j, \ud83d\ude42 &#8211; x);<code><br \/>\nif d &lt; norm(points(j, :) - centroids(clusters(j), :))<br \/>\nclusters(j) = i;<br \/>\nend<br \/>\nend<\/code><\/p>\n<p>% Update the centroid for this cluster.<br \/>\ncentroids(i, \ud83d\ude42 = x;<br \/>\nend<br \/>\n% Remove any unused centroids.<br \/>\ncentroids = centroids(any(clusters == (1:length(E))&#8217;, 1), :);<br \/>\nend<\/p>\n","protected":false},"excerpt":{"rendered":"<p>matlab \u7ef4\u8bfa\u56fe\u8d28\u5fc3\u7b97\u6cd5\u3002 function [centroids, clusters] = voronoi_centroid(points) %VORONOI_CENTROID Vornoi d&#8230; <\/p>\n","protected":false},"author":1,"featured_media":809,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,25],"tags":[],"class_list":["post-804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uav","category-25"],"_links":{"self":[{"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=804"}],"version-history":[{"count":5,"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions"}],"predecessor-version":[{"id":998,"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions\/998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=\/wp\/v2\/media\/809"}],"wp:attachment":[{"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autouav.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}