# polymerge

Merge line segments with matching endpoints

## Syntax

```[latMerged, lonMerged] = polymerge(lat, lon) [latMerged, lonMerged] = polymerge(lat, lon, tol) [latMerged, lonMerged] = polymerge(lat, lon, tol, outputFormat) ```

## Description

```[latMerged, lonMerged] = polymerge(lat, lon)``` accepts a multipart line in latitude-longitude with vertices stored in arrays `lat` and `lon`, and merges the parts wherever a pair of end points coincide. For this purpose, an end point can be either the first or last vertex in a given part. When a pair of parts are merged, they are combined into a single part and the duplicate common vertex is removed. If two first vertices coincide or two last vertices coincide, then the vertex order of one of the parts will be reversed. A merge is applied anywhere that the end points of exactly two distinct parts coincide, so that an indefinite number of parts can be chained together in a single call to `polymerge`. If three or more distinct parts share a common end point, however, the choice of which parts to merge is ambiguous and therefore none of the corresponding parts are connected at that common point.

The inputs `lat` and `lon` can be column or row vectors with NaN-separated parts (and identical NaN locations in each array), or they can be cell arrays with each part in a separate cell. The form of the output arrays, `latMerged` and `lonMerged`, matches the inputs in this regard.

```[latMerged, lonMerged] = polymerge(lat, lon, tol)``` combines line segments whose endpoints are separated by less than the circular tolerance, `tol`. `tol` has the same units as the polygon input.

```[latMerged, lonMerged] = polymerge(lat, lon, tol, outputFormat)``` allows you to request either the NaN-separated vector form for the output (set `outputFormat` to `'vector'`), or the cell array form (set `outputFormat` to `'cell'`).

## Examples

collapse all

Construct column vectors representing coordinate values. The vectors use `NaN` separators to define four line segments.

```lat = [3 2 NaN 1 2 NaN 5 6 NaN 3 4]'; lon = [13 12 NaN 11 12 NaN 15 16 NaN 13 14]';```

Concatenate the segments with matching endpoints. Three of the line segments have overlapping end points, so `polymerge` returns two line segments.

`[latm, lonm] = polymerge(lat,lon)`
```latm = 8×1 1 2 3 4 NaN 5 6 NaN ```
```lonm = 8×1 11 12 13 14 NaN 15 16 NaN ```

## Version History

Introduced before R2006a