【Matlab】基于KDtree的最近邻搜索和范围搜索

本文介绍了Matlab中用于查找指定距离范围内数据点的rangesearch函数和寻找最近K个数据点的knnsearch函数,通过实例展示了如何使用这些函数进行近邻搜索和基于KDTree的数据处理。
摘要由CSDN通过智能技术生成

摘要:介绍Matlab的rangesearch()函数和knnsearch()函数。

rangesearch() —— 根据给定k-维数据集,返回指定距离范围内的所有数据点
knnsearch() —— 根据给定k-维数据集,返回最近的K个数据点

 
%% 给定数值矩阵(input data),返回最近点的K个点
% data matrix,100 x 3,表示100个空间点
data = [hospital.Age, hospital.Weight, 66 * rand(100, 1)];
% 绘制空间点
scatter3(data(:, 1), data(:, 2), data(:, 3), 36, [1 0 0], "filled", "o")
hold on
zlabel 'Z'
% 目标点有两个
targetPoint = [50 162 21;
12 180 31];
% 绘制目标点
scatter3(targetPoint(:, 1), targetPoint(:, 2), targetPoint(:, 3), 72, "magenta", "filled", "hexagram")
hold on
tic
% 搜索目标点周围最近的3个点,返回值为data matrix的row index.
id = knnsearch(data, targetPoint, "Distance", "euclidean", "K", 3, "NSMethod", "kdtree");
toc
result = data(reshape(id,[1,6]), :);
% 绘制result 点
scatter3(result(:, 1), result(:, 2),result(:,3), 72, "blue", "filled", "square")

基于近邻分类的实例选择算法 

img

 
%% rangesearch():返回指定距离内的所有行索引
% dataCloud是10维数据,545580个数据点。
data = dataCloud(:,1:3);
% 绘制散点
scatter3(data(:, 1), data(:, 2), data(:, 3), 12, [1 0 0], "filled", "o")
hold on
zlabel 'Z'
targetPoint = [40164.5 139.539 496.914];
% 绘制目标点
scatter3(targetPoint(:, 1), targetPoint(:, 2), targetPoint(:, 3), 36, "magenta", "filled", "hexagram")
hold on
tic
% knn search using input data,直接给定数据集,搜索半径为50
id = rangesearch(data(:, 1 : 3), targetPoint, 50, "NSMethod", "kdtree", "Distance", "euclidean");
toc
result = data(id{1}, :);
% 绘制result 点
scatter3(result(:, 1), result(:, 2), result(:, 3), 72, "blue", "filled", "square")

img

 
%% 先创建KDtree model ,以此为基础返回范围内的点
data = dataCloud;
% 绘制散点
scatter3(data(:, 1), data(:, 2), data(:, 3), 12, [1 0 0], "filled", "o")
hold on
zlabel 'Z'
targetPoint = [40235.1142325237 -128.940013761462 738.5117255760920];
% 绘制目标点
scatter3(targetPoint(:, 1), targetPoint(:, 2), targetPoint(:, 3), 36, "magenta", "filled", "hexagram")
hold on
mdl = KDTreeSearcher(data(:, 1 : 3), "Distance", 'euclidean');
tic
% knn search using KDtree model,直接给定数据集,搜索半径为50
id = rangesearch(mdl, targetPoint, 50, "Distance", "euclidean");
toc
result = data(id{1}, :);
% 绘制result 点
scatter3(result(:, 1), result(:, 2), result(:, 3), 72, "blue", "filled", "square")

img

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值