Define a square region with a square hole. Specify the vertices of the outer loop in a counterclockwise direction, and specify the vertices for the inner loop in a clockwise direction. Use NaN to separate the coordinates for the outer and inner loops.

xv = [1 4 4 1 1 NaN 2 2 3 3 2];
yv = [1 1 4 4 1 NaN 2 3 3 2 2];

Define x and y coordinates of 500 random points. Initialize the random-number generator to make the output of randn repeatable.

rng default
xq = rand(500,1)*5;
yq = rand(500,1)*5;

Determine whether each point lies inside or on the edge of the polygon area.

in = inpolygon(xq,yq,xv,yv);

Plot the polygon and the query points. Display the points inside the polygon with a red plus. Display the points outside the polygon with a blue circle.

Determine whether each point lies inside or on the edge of the polygon area. Also determine whether any of the points lie on the edge of the polygon area.

[in,on] = inpolygon(xq,yq,xv,yv);

Determine the number of points lying inside or on the edge of the polygon area.

numel(xq(in))

ans =
8

Determine the number of points lying on the edge of the polygon area.

numel(xq(on))

ans =
2

Determine the number of points lying outside the polygon area (not inside or on the edge).

numel(xq(~in))

ans =
4

Plot the polygon and the points. Display the points strictly inside the polygon with a red plus. Display the points on the edge with a black asterisk. Display the points outside the polygon with a blue circle.

To specify vertices of multiply connected or disjoint polygons,
separate the coordinates for distinct loops with NaN.
Additionally for multiply connected polygons, you must orient the
vertices for external and internal loops in opposite directions.

The polygon cannot be self-intersecting and multiply connected
due to the ambiguity associated with self-intersections and loop orientations.

To specify vertices of multiply connected or disjoint polygons,
separate the coordinates for distinct loops with NaN.
Additionally for multiply connected polygons, you must orient the
vertices for external and internal loops in opposite directions.

The polygon cannot be self-intersecting and multiply connected
due to the ambiguity associated with self-intersections and loop orientations.