matlab散点图均匀选取中心点
时间: 2023-07-13 13:05:32 浏览: 120
可以使用以下步骤在 Matlab 中实现散点图的均匀选取中心点:
1. 生成散点图数据,例如:
```
x = randn(1000,1);
y = randn(1000,1);
scatter(x,y);
```
2. 设置选取中心点的数量和边界范围,例如:
```
num_centers = 10;
x_min = -3;
x_max = 3;
y_min = -3;
y_max = 3;
```
3. 计算每个中心点的间隔,例如:
```
x_step = (x_max - x_min) / num_centers;
y_step = (y_max - y_min) / num_centers;
```
4. 使用循环遍历所有中心点并保存它们的坐标,例如:
```
centers = zeros(num_centers,2);
for i = 1:num_centers
x_center = x_min + (i-1)*x_step + x_step/2;
for j = 1:num_centers
y_center = y_min + (j-1)*y_step + y_step/2;
centers((i-1)*num_centers+j,:) = [x_center y_center];
end
end
```
5. 绘制所有中心点,例如:
```
hold on;
scatter(centers(:,1),centers(:,2),'r','filled');
```
完整代码如下:
```
x = randn(1000,1);
y = randn(1000,1);
scatter(x,y);
num_centers = 10;
x_min = -3;
x_max = 3;
y_min = -3;
y_max = 3;
x_step = (x_max - x_min) / num_centers;
y_step = (y_max - y_min) / num_centers;
centers = zeros(num_centers,2);
for i = 1:num_centers
x_center = x_min + (i-1)*x_step + x_step/2;
for j = 1:num_centers
y_center = y_min + (j-1)*y_step + y_step/2;
centers((i-1)*num_centers+j,:) = [x_center y_center];
end
end
hold on;
scatter(centers(:,1),centers(:,2),'r','filled');
```
阅读全文