sortregions

Reorders the regions of a polyshape by a chosen criterion.

Category: Mathematics > Computational Geometry

Syntax

pshape = sortregions(pshape, criterion)

Sorts the regions in ascending order of the criterion.

pshape = sortregions(pshape, criterion, direction)

Sorts in 'ascend' or 'descend' order.

Description

It works like sortboundaries, but treats a region together with its holes as one unit. The order matters for functions that take a region index, such as regions(pshape, I) and area(pshape, I).

Input arguments

criterion
'area', 'perimeter', 'numsides' or 'centroid'.
direction
'ascend' (default) or 'descend'.

Output arguments

pshape
The same shape with its regions stored in the new order; each hole stays with its region.

Example

Order two islands by area

P = polyshape([0 1 1 0 NaN 3 5 5 3], [0 0 1 1 NaN 0 0 2 2]);
S = sortregions(P, 'area', 'ascend');
S.Vertices
Output
ans = 9×2

     0     0
     0     1
     1     1
     1     0
   NaN   NaN
     3     0
     3     2
     5     2
     5     0

See also

sortboundaries regions rmboundary area