📄 srchbbt1nn.m
字号:
function [NNINDEX, NNDIST, DISTCOMPCOUNT] = srchbbt1nn(vec, tree, alldata)
% SRCHBBT1NN Branch-and-bound tree search for 1 nearest neighbor.
% Usage: [NNINDEX, NNDIST, DISTCOMPCOUNT] = srchbbt1nn(vec, tree, alldata)
% vec: test input vector
% tree: tree structure generated by genBBT.m
% alldata: all sample data points
% NNINDEX: index of the nearest neighbor
% NNDIST: distance to the nearest neighbor
% DISTCOMPCOUNT: no. of distance computation
%
% Field of tree structure:
% tree(i).mean: mean vector of a tree node
% tree(i).radius: radius vector of a tree node
% tree(i).child: indices of children for a non-terminal node
% tree(i).data: indices of data for a terminal node
% tree(i).dist2mean: distance to mean of a terminal node
%
% See also GENBBT, TRAVERSE.
% Roger Jang, 20000114
global NNINDEX % Nearest neighbor index
global NNDIST % Nearest neighbor distance
global DISTCOMPCOUNT % No. of distance computation
NNINDEX = nan;
NNDIST = inf;
DISTCOMPCOUNT = 0;
treesearch(vec, tree, 1, alldata);
% ====== Definition of treesearch() subfunction
function treesearch(vec, tree, index, alldata)
global NNINDEX NNDIST DISTCOMPCOUNT
% ====== According to rule 1
if distance(vec, tree(index).mean) >= NNDIST+tree(index).radius,
% fprintf('Node %g is skipped.\n', index);
return;
end
if ~isempty(tree(index).child),
% ====== Recursion into the child nodes
for i=tree(index).child,
treesearch(vec, tree, i, alldata);
end
else
% ====== Check each data item
dataindex = tree(index).data;
dist2mean = tree(index).dist2mean;
for i = 1:length(dataindex),
% ====== According to rule 2
if distance(vec, tree(index).mean) < NNDIST+dist2mean(i),
temp = distance(vec, alldata(dataindex(i), :));
if temp < NNDIST,
NNDIST = temp;
NNINDEX = dataindex(i);
end
DISTCOMPCOUNT = DISTCOMPCOUNT + 1;
end
end
end
% ====== Definition of distance() subfunction
function out = distance(vec1, vec2)
out = norm(vec1-vec2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -