Main Content

使用容差为散点数据分组

本示例展示如何根据散点数据点与相关点的邻近度对其进行分组。

创建一个随机二维点集。然后创建并在随机数据的上方绘制等间距点网格。

x = rand(10000,2);
[a,b] = meshgrid(0:0.1:1);
gridPoints = [a(:), b(:)];
plot(x(:,1), x(:,2), '.')
hold on
plot(gridPoints(:,1), gridPoints(:,2), 'xr', 'Markersize', 6)

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

使用 ismembertol,在 x 内查找处于 gridPoints 中的网格点容差范围内的数据点。将以下选项与 ismembertol 配合使用:

  • ByRows 指定为 true,因为点坐标在 x 的行中。

  • OutputAllIndices 指定为 true,以返回 x 中处于 gridPoints 对应行容差范围内的行的所有索引。

[LIA,LocB] = ismembertol(gridPoints, x, 0.05, ...
    'ByRows', true, 'OutputAllIndices', true);

针对每个网格点,标绘 x 中处于该网格点容差范围内的点。

figure
hold on
for k = 1:length(LocB)
    plot(x(LocB{k},1), x(LocB{k},2), '.')
end
plot(gridPoints(:,1), gridPoints(:,2), 'xr', 'Markersize', 6)

Figure contains an axes object. The axes object contains 122 objects of type line.

另请参阅

相关主题