# How to Merge Two Sets of Data in MATLAB

The MATLAB programming language offers predefined functions for merging sets of data into a new set or array. With a few lines of simple code, you can merge sets of data into a sorted array or matrix. A mathematical set contains no more than one of each member in the set, so you’ll need to decide if the return value of your operation should also be a set or possibly contain repeated values. Concatenating two sets and sorting the result will yield an array containing all members of the original sets.

#### 1

Load two sets of data into the MATLAB workspace with the “load” command or by creating new sets from existing arrays. Use array functions such as “union,” “intersect” or “setdiff” to create a set from two array variables as in the following lines:

set1 = union(array1, array2); set2 = setdiff(array1, array2);

#### 2

Concatenate sets with the “cat” function, which combines them into one array ordered head to tail. For example, if “set1” contains “[2, 4, 5]” and “set2” contains “[1, 3, 6]” then concatenating them will return the array “[2, 4, 5, 1, 3, 6].” The “cat” function also takes an array dimension as the first parameter, as in the following command:

array3 = cat(1, set1, set2);

The variable “array3” will be a one-dimensional array in this case.

#### 3

Sort an array with the “sort” command, which sorts values in ascending order by default. The “sort” command puts arrays of character types in ASCII order. When sorted in this way, numbers and some special characters come before letters, and uppercase letters come before lowercase letters.

#### 4

Merge two sets of unlike data types just as you would merge like data types. For example, when you concatenate an array of strings and an array of integers, MATLAB converts the integers to a character type. Sorting the result puts the numbers before the letters, or you can sort them with the “descend” keyword to put them in reverse order as in the following:

mixed_array = cat(1, char_array, num_array); sort(mixed_array, 'descend');

The “descend” keyword goes inside a pair of single quotes.

#### 5

Enclose two sets of data in square brackets to concatenate them using fewer keystrokes. Square brackets have the same meaning in MATLAB as the “cat” function but with a somewhat different syntax. The colon and semicolon operators reshape your arrays as column vectors and multidimensional arrays, respectively. For example, to merge two sets of data into a two-dimensional array, use the semicolon operator as in the following line:

array2d = [set1; set2];

Sorting this array will have the same result as sorting the sets separately before concatenating them. The “sort” function will keep the rows of the multidimensional array separate.

Resources

Tips

- Save the merged data in a MATLAB file by using the “save” command. Specify only a filename to save all variables currently loaded in the workspace. Add the variable names of the values to save if you want to save only certain values. For example, to save the variable “array2d” in a file called “merged_sets,” use the following command:
- save(merged_sets, array2d);
- Load the file back into the workspace with the following command:
- load(merged_sets);

Writer Bio

David Wayne has been writing since 2010, with technology columns appearing in several regional newspapers in Texas. Wayne graduated from the University of Houston in 2005, earning a Bachelor of Arts in communications.