📄 nndemo.html
字号:
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>NEARESTNEIGHBOUR Demonstration</title> <meta name="generator" content="MATLAB 7.1"> <meta name="date" content="2006-12-13"> <meta name="m-file" content="nndemo"><style>body { background-color: white; margin:10px;}h1 { color: #990000; font-size: x-large;}h2 { color: #990000; font-size: medium;}/* Make the text shrink to fit narrow windows, but not stretch too far in wide windows. On Gecko-based browsers, the shrink-to-fit doesn't work. */ p,h1,h2,div.content div { /* for MATLAB's browser */ width: 600px; /* for Mozilla, but the "width" tag overrides it anyway */ max-width: 600px; /* for IE */ width:expression(document.body.clientWidth > 620 ? "600px": "auto" );}pre.codeinput { background: #EEEEEE; padding: 10px;}span.keyword {color: #0000FF}span.comment {color: #228B22}span.string {color: #A020F0}span.untermstring {color: #B20000}span.syscmd {color: #B28C00}pre.codeoutput { color: #666666; padding: 10px;}pre.error { color: red;}p.footer { text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;} </style></head> <body> <div class="content"> <h1>NEARESTNEIGHBOUR Demonstration</h1> <introduction> <p>This (publishable) script <tt>nndemo.m</tt> demonstrates the features of the nearestneighbour function. </p> </introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">Summary</a></li> <li><a href="#3">Example 1: Basic nearest neighbour search</a></li> <li><a href="#4">Example 2: Nearest neighbour to subset of a point set</a></li> <li><a href="#5">Example 3: Finding nearest n neighbours</a></li> <li><a href="#6">Example 4: DelaunayMode - Large Data set, few points to lookup</a></li> <li><a href="#7">Example 5: DelaunayMode - smallish data set, lots of points to lookup</a></li> </ul> </div> <h2>Summary<a name="1"></a></h2> <p><tt>nearestneighbour</tt> computes nearest neighbours to a set of points from a set of candidate points by Euclidean distance. Points are considered to be matrix columns. It has a number of convenient features which might make it worth using rather than simply coding a couple-of-lines search into an m-file. These include: </p> <div> <ul> <li>Specifying points as either a separate matrix, or as indices into the candidate matrix</li> <li>Finding Multiple Neighbours</li> <li>Automatically using Delaunay Triangulation if it would be faster</li> </ul> </div> <h2>Example 1: Basic nearest neighbour search<a name="3"></a></h2><pre class="codeinput"><span class="comment">% 2D points of interest</span>P = rand(2, 5);<span class="comment">% Candidate point set</span>X = rand(2, 50);I = nearestneighbour(P, X);disp(<span class="string">'Candidate points:'</span>)disp(P)disp(<span class="string">'Nearest neighbours'</span>)disp(X(:, I))<span class="comment">% Plot the points</span>plot(X(1,:), X(2,:), <span class="string">'b.'</span>, P(1,:), P(2,:), <span class="string">'r.'</span>, <span class="string">'MarkerSize'</span>, 15);hold <span class="string">on</span>quiver(P(1, :), P(2, :), X(1,I) - P(1, :), X(2, I) - P(2, :), 0, <span class="string">'k'</span>);hold <span class="string">off</span></pre><pre class="codeoutput">Candidate points: 0.3451 0.5054 0.4258 0.6476 0.4602 0.3632 0.6416 0.1706 0.2709 0.2320Nearest neighbours 0.2923 0.4611 0.4275 0.7098 0.4283 0.2685 0.6354 0.0914 0.2932 0.2683</pre><img vspace="5" hspace="5" src="nndemo_01.png"> <h2>Example 2: Nearest neighbour to subset of a point set<a name="4"></a></h2> <p>Sometimes the features of interest will be certain members of a set, and you want to find the closest point(s) from within the set </p><pre class="codeinput"><span class="comment">% Candidate 4D feature set</span>X = rand(4, 20);<span class="comment">% Find nearest neighbour to the 2nd, 4th, 10th features</span>I = nearestneighbour([2 4 10], X)<span class="comment">% Find nearest neighbours to all columns of X</span>I = nearestneighbour(X)</pre><pre class="codeoutput">I = 20 20 14I = 10 20 5 20 15 19 8 7 12 14 18 9 18 10 5 9 11 11 6 2</pre><h2>Example 3: Finding nearest n neighbours<a name="5"></a></h2> <p>Let's calculate the four nearest neighbours to a point</p><pre class="codeinput"><span class="comment">% three points of interest</span>P = rand(2, 3);<span class="comment">% Candidate feature set</span>X = rand(2, 50);<span class="comment">% Calculate the 4 nearest neighbours to each point: I(:, 1) will be the 4</span><span class="comment">% nearest neighbours to P(:, 1), etc.</span>I = nearestneighbour(P, X, <span class="string">'NumberOfNeighbours'</span>, 4)<span class="comment">% Plot the points - show the neighbours for one of them</span>plot(P(1,:), P(2, :), <span class="string">'r.'</span>, X(1,:), X(2,:), <span class="string">'b.'</span>, <span class="string">'MarkerSize'</span>, 15)hold <span class="string">on</span>p1 = repmat(P(1,1), 1, 4); p2 = repmat(P(2,1), 1, 4);quiver(p1, p2, X(1, I(:, 1)) - p1, X(2, I(:, 1)) - p2, 0, <span class="string">'k'</span>)hold <span class="string">off</span></pre><pre class="codeoutput">I = 21 1 48 37 14 5 17 33 38 41 12 39</pre><img vspace="5" hspace="5" src="nndemo_02.png"> <h2>Example 4: DelaunayMode - Large Data set, few points to lookup<a name="6"></a></h2> <p>We'll run a couple of examples and show how <tt>nearestneighbour</tt> automatically works out when to use the Delaunay triangulation. You may need to tweak the numbers of zeros to get meaningful results on your computer. First we'll try a large data set with few points to lookup - it shouldn't use Delaunay Triangulation as the overhead required to compute this should definitely offset any gains. We'll run <tt>nearestneighbour</tt> with DelaunayMode 'on', 'off', and 'auto'. Note that the numbers are fairly conservative to avoid crashing slow computers with this demo! </p><pre class="codeinput"><span class="comment">% Large Data set, few points to look up</span>P = rand(3, 10);X = rand(3, 5000);fprintf(<span class="string">'Delaunay mode test: Large data set, few candidate points:\n'</span>)<span class="comment">% Run with DelaunayMode off</span>fprintf(<span class="string">'Elapsed Time (DelaunayMode off) : %.2g seconds\n'</span>, <span class="keyword">...</span> timingtest(P, X, <span class="string">'off'</span>))<span class="comment">% Run with DelaunayMode on</span>fprintf(<span class="string">'Elapsed Time (DelaunayMode on) : %.2g seconds\n'</span>, <span class="keyword">...</span> timingtest(P, X, <span class="string">'on'</span>))<span class="comment">% Run with DelaunayMode automatic - note 'auto' is the default</span>fprintf(<span class="string">'Elapsed Time (DelaunayMode auto): %.2g seconds\n\n'</span>, <span class="keyword">...</span> timingtest(P, X, <span class="string">'auto'</span>))</pre><pre class="codeoutput">Delaunay mode test: Large data set, few candidate points:Elapsed Time (DelaunayMode off) : 0.0047 secondsElapsed Time (DelaunayMode on) : 0.79 secondsElapsed Time (DelaunayMode auto): 0.0047 seconds</pre><h2>Example 5: DelaunayMode - smallish data set, lots of points to lookup<a name="7"></a></h2> <p>If there are lots of points to lookup, however, using the Delaunay Triangulation should be an advantage</p><pre class="codeinput"><span class="comment">% Small Data set, lots of candidate points</span>P = rand(3, 10000);X = rand(3, 500);fprintf(<span class="string">'Delaunay mode test: Small data set, many candidate points:\n'</span>)<span class="comment">% Run with DelaunayMode off</span>fprintf(<span class="string">'Elapsed Time (DelaunayMode off) : %.2g seconds\n'</span>, <span class="keyword">...</span> timingtest(P, X, <span class="string">'off'</span>))<span class="comment">% Run with DelaunayMode on</span>fprintf(<span class="string">'Elapsed Time (DelaunayMode on) : %.2g seconds\n'</span>, <span class="keyword">...</span> timingtest(P, X, <span class="string">'on'</span>))<span class="comment">% Run with DelaunayMode automatic</span>fprintf(<span class="string">'Elapsed Time (DelaunayMode auto): %.2g seconds\n'</span>, <span class="keyword">...</span> timingtest(P, X, <span class="string">'auto'</span>))</pre><pre class="codeoutput">Delaunay mode test: Small data set, many candidate points:Elapsed Time (DelaunayMode off) : 0.67 secondsElapsed Time (DelaunayMode on) : 0.16 secondsElapsed Time (DelaunayMode auto): 0.16 seconds</pre><p class="footer"><br> Published with MATLAB® 7.1<br></p> </div> <!--##### SOURCE BEGIN #####%% NEARESTNEIGHBOUR Demonstration
% This (publishable) script |nndemo.m| demonstrates the features of the
% nearestneighbour function.
%% Summary
% |nearestneighbour| computes nearest neighbours to a set of points from a
% set of candidate points by Euclidean distance. Points are considered to
% be matrix columns.
% It has a number of convenient features which might make it worth using
% rather than simply coding a couple-of-lines search into an m-file. These
% include:
%
%%
%
% * Specifying points as either a separate matrix, or as indices into the
% candidate matrix
% * Finding Multiple Neighbours
% * Automatically using Delaunay Triangulation if it would be faster
%
%% Example 1: Basic nearest neighbour search
% 2D points of interest
P = rand(2, 5);
% Candidate point set
X = rand(2, 50);
I = nearestneighbour(P, X);
disp('Candidate points:')
disp(P)
disp('Nearest neighbours')
disp(X(:, I))
% Plot the points
plot(X(1,:), X(2,:), 'b.', P(1,:), P(2,:), 'r.', 'MarkerSize', 15);
hold on
quiver(P(1, :), P(2, :), X(1,I) - P(1, :), X(2, I) - P(2, :), 0, 'k');
hold off
%% Example 2: Nearest neighbour to subset of a point set
% Sometimes the features of interest will be certain members of a set, and
% you want to find the closest point(s) from within the set
% Candidate 4D feature set
X = rand(4, 20);
% Find nearest neighbour to the 2nd, 4th, 10th features
I = nearestneighbour([2 4 10], X)
% Find nearest neighbours to all columns of X
I = nearestneighbour(X)
%% Example 3: Finding nearest n neighbours
% Let's calculate the four nearest neighbours to a point
% three points of interest
P = rand(2, 3);
% Candidate feature set
X = rand(2, 50);
% Calculate the 4 nearest neighbours to each point: I(:, 1) will be the 4
% nearest neighbours to P(:, 1), etc.
I = nearestneighbour(P, X, 'NumberOfNeighbours', 4)
% Plot the points - show the neighbours for one of them
plot(P(1,:), P(2, :), 'r.', X(1,:), X(2,:), 'b.', 'MarkerSize', 15)
hold on
p1 = repmat(P(1,1), 1, 4); p2 = repmat(P(2,1), 1, 4);
quiver(p1, p2, X(1, I(:, 1)) - p1, X(2, I(:, 1)) - p2, 0, 'k')
hold off
%% Example 4: DelaunayMode - Large Data set, few points to lookup
% We'll run a couple of examples and show how |nearestneighbour|
% automatically works out when to use the Delaunay triangulation.
% You may need to tweak the
% numbers of zeros to get meaningful results on your computer.
% First we'll try a large data set with few points to lookup - it shouldn't use Delaunay
% Triangulation as the overhead required to compute this should definitely
% offset any gains. We'll run |nearestneighbour| with DelaunayMode 'on',
% 'off', and 'auto'.
% Note that the numbers are fairly conservative to avoid crashing slow
% computers with this demo!
% Large Data set, few points to look up
P = rand(3, 10);
X = rand(3, 5000);
fprintf('Delaunay mode test: Large data set, few candidate points:\n')
% Run with DelaunayMode off
fprintf('Elapsed Time (DelaunayMode off) : %.2g seconds\n', ...
timingtest(P, X, 'off'))
% Run with DelaunayMode on
fprintf('Elapsed Time (DelaunayMode on) : %.2g seconds\n', ...
timingtest(P, X, 'on'))
% Run with DelaunayMode automatic - note 'auto' is the default
fprintf('Elapsed Time (DelaunayMode auto): %.2g seconds\n\n', ...
timingtest(P, X, 'auto'))
%% Example 5: DelaunayMode - smallish data set, lots of points to lookup
% If there are lots of points to lookup, however, using the Delaunay
% Triangulation should be an advantage
% Small Data set, lots of candidate points
P = rand(3, 10000);
X = rand(3, 500);
fprintf('Delaunay mode test: Small data set, many candidate points:\n')
% Run with DelaunayMode off
fprintf('Elapsed Time (DelaunayMode off) : %.2g seconds\n', ...
timingtest(P, X, 'off'))
% Run with DelaunayMode on
fprintf('Elapsed Time (DelaunayMode on) : %.2g seconds\n', ...
timingtest(P, X, 'on'))
% Run with DelaunayMode automatic
fprintf('Elapsed Time (DelaunayMode auto): %.2g seconds\n', ...
timingtest(P, X, 'auto'))##### SOURCE END #####--> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -