nearestestimate.m
来自「脱机手写体识别Matlab源程序 包括特征提取、bayes分类器、K近邻分类及」· M 代码 · 共 19 行
M
19 行
% 最近邻估计
% 直接通过测试样本估计
function [Nearest]=NearestEstimate(trainX,trainY,testX)
trainCnt=size(trainX,2); % 训练样本数量
testCnt=size(testX,2); % 测试样本数量
Nearest=zeros(1,testCnt); % 最近邻估计结果
for i=1:testCnt
curSample=testX(:,i);
dist=zeros(1,trainCnt);
for j=1:trainCnt % 计算与每个测试样本的欧式距离
dist(j)=sqrt(sum((curSample-trainX(:,j)).^2));
end
[minDist minIndex]=min(dist);
Nearest(i)=trainY(minIndex); % 取欧式距离最小值的样本标号
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?