Main Content

使用容差为类似数据点求平均值

此示例说明如何使用 uniquetol 来为具有类似(在容差范围内)的 xy 坐标的三维点求平均 z 坐标。

使用通过 peaks 函数在 [-3,3]×[-3,3] 域内选取的随机点作为数据集。向数据中添加少量干扰。

xy = rand(10000,2)*6-3; 
z = peaks(xy(:,1),xy(:,2)) + 0.5-rand(10000,1);
A = [xy z];
plot3(A(:,1), A(:,2), A(:,3), '.')
view(-28,32)

Figure contains an axes object. The axes contains a line object which displays its values using only markers.

使用以下选项,通过 uniquetol 查找具有类似 xy 坐标的点:

  • ByRows 指定为 true,因为 A 的行包含点坐标。

  • OutputAllIndices 指定为 true,以返回在彼此容差范围内的所有点的索引。

  • DataScale 指定为 [1 1 Inf],以将绝对容差用于 xy 坐标,同时忽略 z 坐标。

DS = [1 1 Inf];
[C,ia] = uniquetol(A, 0.3, 'ByRows', true, ...
    'OutputAllIndices', true, 'DataScale', DS);

为在容差范围内的每组点求平均值(包括 z 坐标),从而生成仍保持原始数据大致形状的约简数据集。

for k = 1:length(ia)
    aveA(k,:) = mean(A(ia{k},:),1); 
end

在原始数据之上标绘生成的平均点。

hold on
plot3(aveA(:,1), aveA(:,2), aveA(:,3), '.r', 'MarkerSize', 15)

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

另请参阅

相关主题