随笔 - 1762  文章 - 0  评论 - 109  阅读 - 431万

matlab查找最临近搜索knnsearch

Idx = knnsearch(X,Y) finds the nearest neighbor in X for each query point in Y and returns the indices of the nearest neighbors in Idx, a column vector. Idx has the same number of rows as Y.

为Y中的每个查询点在X中查找最近的邻居,并返回IDX(列向量)中最近邻居的索引。IDX的行数与y相同。

Idx = knnsearch(X,Y,Name,Value) returns Idx with additional options specified using one or more name-value pair arguments. For example, you can specify the number of nearest neighbors to search for and the distance metric used in the search.

返回IDX,并使用一个或多个名称-值对参数指定其他选项。例如,可以指定要搜索的最近邻居的数目和搜索中使用的距离度量。

[Idx,D] = knnsearch(___) additionally returns the matrix D, using any of the input arguments in the previous syntaxes. D contains the distances between each observation in Y and the corresponding closest observations in X.

使用先前语法中的任何输入参数返回矩阵D。D包含Y中每个观测值与X中对应的最近观测值之间的距离。

举例:

Load Fisher's iris data set.

load fisheriris
X = meas(:,3:4); % Measurements of original flowers
Y = [5 1.45;6 2;2.75 .75]; % New flower data

Perform a knnsearch between X and the query points Y using Minkowski and Chebychev distance metrics.

使用Minkowski和chebychev距离度量在x和查询点y之间执行 knnsearch

[mIdx,mD] = knnsearch(X,Y,'K',10,'Distance','minkowski','P',5);
[cIdx,cD] = knnsearch(X,Y,'K',10,'Distance','chebychev');

Visualize the results of the two nearest neighbor searches. Plot the training data. Plot the query points with the marker X. Use circles to denote the Minkowski nearest neighbors. Use pentagrams to denote the Chebychev nearest neighbors.

可视化两个最近邻搜索的结果。绘制训练数据。用标记x绘制查询点。用圆表示Minkowski最近的邻居。用五角星表示切比雪夫最近的邻居。

gscatter(X(:,1),X(:,2),species);
line(Y(:,1),Y(:,2),'Marker','x','Color','k',...
'Markersize',10,'Linewidth',2,'Linestyle','none');
line(X(mIdx,1),X(mIdx,2),'Color',[.5 .5 .5],'Marker','o',...
'Linestyle','none','Markersize',10);
line(X(cIdx,1),X(cIdx,2),'Color',[.5 .5 .5],'Marker','p',...
'Linestyle','none','Markersize',10);
legend('setosa','versicolor','virginica','query point',...
'minkowski','chebychev','Location','best');





来源:https://ww2.mathworks.cn/help/stats/knnsearch.html?searchHighlight=knnsearch&s_tid=doc_srchtitle
posted on   一杯明月  阅读(4044)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示