Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# intersect

Class: categorical

Set intersection for categorical arrays

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.

## Syntax

C = intersect(A,B)
[C,IA,IB] = intersect(A,B)
[...] = intersect(A,B,'rows')
[...] = intersect(...,'legacy')
[...] = intersect(A,B,setOrder)
[...] = intersect(A,B,'rows',setOrder)

## Description

C = intersect(A,B) for categorical vectors A and B, returns a categorical vector C containing the values common to the two vectors with no repetitions. The result C is sorted. The set of categorical levels for C is the sorted union of the sets of levels of the inputs.

[C,IA,IB] = intersect(A,B) also returns index vectors IA and IB such that C = A(IA) and C = B(IB). If there are repeated common values in A or B, then the index of the last occurrence of each repeated value is returned.

[...] = intersect(A,B,'rows') for categorical matrices A and B with the same number of columns, returns the rows common to the two matrices. The rows of the matrix C are sorted. The set of categorical levels for C is the sorted union of the sets of levels of the inputs. The optional outputs IA and IB are index vectors such that C = A(IA,:) and C = B(IB,:).

[...] = intersect(...,'legacy') preserves the pre-2012a behavior of intersect. You can specify the flag as the final argument with any previous syntax that accepts A, B, or 'rows'.

[...] = intersect(A,B,setOrder) and [...] = intersect(A,B,'rows',setOrder) returns the observations of C in a specific order. setOrder='sorted' returns the values or rows of C in sorted order. setOrder='stable' returns the values or rows of C in the same order as A. If A and B are row vectors, then C is also a row vector. Otherwise, C is a column vector. IA and IB are column vectors. If there are repeated common values in A or B, then the index of the first occurrence of each repeated value is returned.