Main Content

union

(Not Recommended) Set union for dataset array observations

The dataset data type is not recommended. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.

Description

C = union(A,B) for dataset arrays A and B returns the combined set of observations from the two arrays, with repetitions removed. The observations in the dataset array C are sorted.

C = union(A,B,vars) returns the combined set of observations from the two arrays, with repetitions of unique combinations of the variables specified in vars removed. The observations in the dataset array C are sorted by those variables.

The values for variables not specified in vars for each observation in C are taken from the corresponding observation in A or B, or from A if there are common observations in both A and B. If there are multiple observations in A or B that correspond to an observation in C, those values are taken from the first occurrence.

C = union(A,B,vars,setOrder) returns the observations in C in the order specified by setOrder.

[C,iA,iB] = union(___) also returns index vectors iA and iB such that C is a sorted combination of the values A(iA,:) and B(iB,:). If there are common observations in A and B, then union returns only the index from A, in iA. If there are repeated observations in A or B, then the index of the first occurrence is returned. You can use any of the previous input arguments.

Input Arguments

collapse all

Input arrays, specified as dataset objects.

Variable names, specified as a string array, cell array of character vectors, or vector of integers containing variable column numbers. vars indicates the variables in A and B that union considers.

Specify vars as [] to use its default value of all variables.

Flag indicating sorting order for observations in the resulting array C, specified as 'sorted' or 'stable'.

'sorted'Observations in C are in sorted order (default).
'stable'Observations in C are in the same order that they appear in A, then B.

Output Arguments

collapse all

Dataset array with the combined observations of A and B, with repetitions removed, returned as a dataset object. C is in sorted order (by default), or the order specified by setOrder.

Index vector indicating observations in A that contribute to the union, returned as a vector of integers. The vector iA contains the index to the first occurrence of any repeated observations in A.

Index vector indicating observations in B that contribute to the union, returned as a vector of integers. The vector iB contains the index to the first occurrence of any repeated observations in B.

Version History

Introduced in R2012b