Thread Subject: Nearest Neighbour Interpolation

Subject: Nearest Neighbour Interpolation

From: Florian

Date: 8 Aug, 2007 09:04:54

Message: 1 of 4

Hello all,

I want to do a simple nearest neighbour interpolation with
out using griddata. Can some one help, please - delauney
throughs errors (see below) and gives no triangulation.

Example:
x=[1 2.5 3 4.5];
y=[1 2.5 3 4.5];
z=[1 2 3 4];

xi=[1.5 2.4];
yi=[1.5 2.5];

what is zi with nearest neighbour interpolation?

Thanks,
flo


Error

>> tri = delaunay(x,y,{'QJ' 'Qbb'});
Warning: qhull precision warning:
The initial hull is narrow (cosine of min. angle is
1.0000000000000000).
A coplanar point may lead to a wide facet. Options 'QbB'
(scale to unit box)
or 'Qbb' (scale last coordinate) may remove this warning.
Use 'Pp' to skip
this warning. See 'Limitations' in qh-impre.htm.

Subject: Nearest Neighbour Interpolation

From: John D'Errico

Date: 8 Aug, 2007 10:56:20

Message: 2 of 4

"Florian " <f.pappenberger@NOSPAMlancater.ac.uk> wrote in message
<f9c0vm$7ct$1@fred.mathworks.com>...
> Hello all,
>
> I want to do a simple nearest neighbour interpolation with
> out using griddata. Can some one help, please - delauney
> throughs errors (see below) and gives no triangulation.
>
> Example:
> x=[1 2.5 3 4.5];
> y=[1 2.5 3 4.5];
> z=[1 2 3 4];
>
> xi=[1.5 2.4];
> yi=[1.5 2.5];
>
> what is zi with nearest neighbour interpolation?
>
> Thanks,
> flo
>
>
> Error
>
> >> tri = delaunay(x,y,{'QJ' 'Qbb'});
> Warning: qhull precision warning:
> The initial hull is narrow (cosine of min. angle is
> 1.0000000000000000).
> A coplanar point may lead to a wide facet. Options 'QbB'
> (scale to unit box)
> or 'Qbb' (scale last coordinate) may remove this warning.
> Use 'Pp' to skip
> this warning. See 'Limitations' in qh-impre.htm.

Sigh.

Look at what you are trying to do. Its always
a good idea. Next, understand the tool that
you are trying to use.

You have placed 4 points in the x,y plane. Where
do they lie? Plot them. Note that for each x, the
value of y is equal to that x. PLOT THEM!!!!!!!

  plot(x,y,'o')
  grid on

Do they form a straight line?

Next, please draw on that plot exactly which
triangulation YOU would form. Remember, the
points fall on a straight line. Feel free to draw
any triangles that you want, as long as the
triangle vertices come from this list of 4 data
points. Feel free to find some non-degnerate
triangles composed of vertices from your data.
A triangle that is degenerate would have zero
area. Triangles with collinear vertices are good
examples of degenerate triangles.

Why are degenerate triangles bad for interpolation?
While obviously, anything that is degenerate must
be BAD TO THE BONE, it turns out that interpolation
inside zero area triangles involves a singularity.
Plus, delaunay has problems of its own, because
IT too cannot decide how to triangulate your data.

I know, griddata should be smart enough to know
that what you are trying to do is not a good thing
to do. Yes, it might decide that even though you
have told it to form a triangulation of this data,
that it could just build a model of z(x), since there
is no additional information in the variable y. But
sadly, griddata is just a lowly, underpaid, working
stiff of a computer program. It tries to do only
what it is designed to do, and no more.

Always think about what you are doing. Understand
the tools that you use. Otherwise you should expect
to see randomly meaningless results.

John

Subject: Nearest Neighbour Interpolation

From: Yonathan Nativ

Date: 8 Aug, 2007 11:13:31

Message: 3 of 4

"Florian " <f.pappenberger@NOSPAMlancater.ac.uk> wrote in
message <f9c0vm$7ct$1@fred.mathworks.com>...
> Hello all,
>
> I want to do a simple nearest neighbour interpolation with
> out using griddata. Can some one help, please - delauney
> throughs errors (see below) and gives no triangulation.
>
> Example:
> x=[1 2.5 3 4.5];
> y=[1 2.5 3 4.5];
> z=[1 2 3 4];
>
> xi=[1.5 2.4];
> yi=[1.5 2.5];
>
> what is zi with nearest neighbour interpolation?
>
> Thanks,
> flo
>

Hi florin

I have a very straightforward solution for you (I don't
think it is an optimal one).

zi = zeros(size(xi));
for n = 1:length(zi(:))
   [ans ind] = min( (x(:)-xi(n)).^2 + (y(:)-yi(n)).^2 );
   zi(n) = z(ind);
end


Y

Subject: Nearest Neighbour Interpolation

From: Florian

Date: 8 Aug, 2007 12:08:48

Message: 4 of 4

Thanks, wanted to avoid the loop.

John, adimittedly badly chosen example - thanks for
providing explanation and a solution,

flo

"Yonathan Nativ" <yonathan_n_REMOVE-THIS@hotmail.com> wrote
in message <f9c8gr$g1j$1@fred.mathworks.com>...
> "Florian " <f.pappenberger@NOSPAMlancater.ac.uk> wrote in
> message <f9c0vm$7ct$1@fred.mathworks.com>...
> > Hello all,
> >
> > I want to do a simple nearest neighbour interpolation with
> > out using griddata. Can some one help, please - delauney
> > throughs errors (see below) and gives no triangulation.
> >
> > Example:
> > x=[1 2.5 3 4.5];
> > y=[1 2.5 3 4.5];
> > z=[1 2 3 4];
> >
> > xi=[1.5 2.4];
> > yi=[1.5 2.5];
> >
> > what is zi with nearest neighbour interpolation?
> >
> > Thanks,
> > flo
> >
>
> Hi florin
>
> I have a very straightforward solution for you (I don't
> think it is an optimal one).
>
> zi = zeros(size(xi));
> for n = 1:length(zi(:))
> [ans ind] = min( (x(:)-xi(n)).^2 + (y(:)-yi(n)).^2 );
> zi(n) = z(ind);
> end
>
>
> Y

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
interpolation Florian 8 Aug, 2007 06:44:41
rssFeed for this Thread

Contact us at files@mathworks.com