Introduction to MATLAB Unique
MATLAB is a programming environment that is interactive and is used in scientific computing. It is extensively used in a lot of technical fields where problem-solving, data analysis, algorithm development, and experimentation is required. The software which are discipline specific are extensively written using MATLAB. These are organized into libraries called toolboxes. MATLAB has also found extensive use in the field of technical education. Where it is used as a base for doing work in computational laboratories.
MATLAB provides us with a lot of mathematical functions, useful in various computational problems. In this article, we will study a powerful MATLAB function called ‘Unique’ As the name suggests, a unique function helps us in getting the ‘unique’ value present in an array.
Syntax:
Let us understand the Syntax of Unique function in MATLAB.
- U = unique(c)
- U = unique(c, setOrder)
- U = unique(c, occurrence)
Now let us understand all these one by one with the help of different examples.
Unique Functions in MATLAB
Below are the functions in MATLAB:
1. U = unique(c)
- This function will result in an array of unique values if the input array has some repeated values in it
- Also, the unique function will sort the output array.
- Unique Function will result in all unique rows of C if C is a table
Let us understand unique(c) with a couple of examples:
This is a simple example without any negative value
c = [1 2 1 6];
This array of numbers is then passed as an argument to our function unique(c)
U= unique(c)
The Output that we obtain will be an array of dimensions 1×3.
This is how our input and output will look like in MATLAB console:
Code:
c = [1 2 1 6];
U= unique(c)
Output:
As we can clearly see in the output, only the unique values are selected from the input array by our Unique function.
Let us now take an example with negative values and see if our function works as expected.
c = [-1 -5 -1 6];
This array consisting of both positive and negative numbers is then passed as an argument to our function unique(c)
U= unique(c)
The Output that we obtain will be an array of dimensions 1×3.
This is how our input and output will look like in MATLAB console:
Code:
c = [-1 -5 -1 6];
U= unique(c)
Output:
One thing to observe in the above 2 examples is that output obtained is sorted. As we have seen in the description of ‘unique’ function, it not only helps us in finding the unique values in the array but also sorts the output obtained.
2. U = unique(c,setorder)
- Here ‘setorder’ can take values as ‘stable’ or ‘sorted’, ‘sorted’ being the default order
- If we keep ‘setorder’ as ‘stable’, the output will be of the order of ‘c’. i.e the sorting will not be performed by ‘unique’ function.
Let us understand this with an example:
A = [1 5 1 2];
In this example, we will keep ‘setorder’ as ‘stable’.
This array of numbers is then passed as an argument to our function unique(c, stable).
C = unique(A,'stable')
The output will be an array of dimensions 1×3.
This is how our input and output will look like in MATLAB console:
Code:
A = [1 5 1 2];
C = unique(A,'stable')
Output:
As we can observe in the output, the values obtained are not sorted and have the default order.
In the next example, we will keep ‘setorder’ as ‘sorted’ with the same input array as in the above example.
Code:
A = [1 5 1 2];
[C] = unique(A, 'sorted')
The output will be an array of dimensions 1×3
This is how our input and output will look like in MATLAB console:
Output:
We can clearly observe the difference here. The values in this example are sorted as expected.
3. U = unique(c,occurrence)
- This function will help us in getting the indices of the repeated values in the input array.
- ’occurrence’ can take 2 values, ‘first’ or ‘last’
- ‘first’ is the default value and will return the ‘first’ index of the repeated value
- ‘last’ will return the ‘last’ index of the repeated value
Let us understand this with an example:
C= [1 1 4 4 4 9]; is our input array
This is how our input and output will look like in MATLAB console:
Input:
[C, iC] = unique([1 1 4 4 4 9], ‘last’)
Output:
As we can observe from the output, we not only get the unique values in the input array, but also the indices of unique values. Here, please observe carefully that since we have passed ‘last’ as an argument, for repeated values, the index obtained is ‘last’ index of the repeated value.
Conclusion
So, in this article, we learned how the unique function works in MATLAB. We can use a unique function to obtain the unique values present in the input array. As an additional feature, a unique function also sorts the output. Although, as we learned, we can control this sorting behavior of the unique function.
Recommended Articles
This is a guide to Matlab Unique. Here we discuss the Introduction to Matlab Unique and its different Examples as well as its Code and output. You can also go through our suggested articles to learn more –