Sorting set of points

1 回表示 (過去 30 日間)
Mahdi Skayni
Mahdi Skayni 2021 年 3 月 29 日
回答済み: AKennedy 2024 年 8 月 19 日
Hello,
I have these set of red points that are saved in one variable. I want to separate them, close one in one variable and so on.
Any idea Please

回答 (1 件)

AKennedy
AKennedy 2024 年 8 月 19 日
To separate the clusters of red points into individual variables in MATLAB, you can use logical indexing or clustering techniques.
  1. Identify the clusters: You can manually identify the clusters based on their coordinates or use a clustering algorithm like "k-means" (https://www.mathworks.com/help/stats/kmeans.html).
  2. Separate the clusters: Use logical indexing to separate the points into different variables. For example:
% Assuming your points are stored in a variable called 'points'
% points is an Nx2 matrix where each row is a point (x, y)
% Example points (replace this with your actual data)
points = [1, 2; 2, 3; 3, 4; 10, 10; 11, 11; 12, 12; 20, 20; 21, 21; 22, 22];
% Number of clusters (adjust based on your data)
numClusters = 3;
% Perform k-means clustering
[idx, C] = kmeans(points, numClusters);
% Separate points into different variables based on cluster index
cluster1 = points(idx == 1, :);
cluster2 = points(idx == 2, :);
cluster3 = points(idx == 3, :);
% Display the clusters
figure;
hold on;
scatter(cluster1(:,1), cluster1(:,2), 'r');
scatter(cluster2(:,1), cluster2(:,2), 'g');
scatter(cluster3(:,1), cluster3(:,2), 'b');
legend('Cluster 1', 'Cluster 2', 'Cluster 3');
hold off;
This code uses "k-means" clustering to separate the points into three clusters. You can adjust the number of clusters ("numClusters") based on your data. Each cluster is then stored in a separate variable ("cluster1", "cluster2", "cluster3").
Adjust the code to fit your specific dataset and requirements.

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by