EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial MATLAB Unique
 

MATLAB Unique

Priya Pedamkar
Article byPriya Pedamkar

MATLAB Unique

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Let us understand the Syntax of Unique function in MATLAB.

  1. U = unique(c)
  2. U = unique(c, setOrder)
  3. 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:

Matlab Unique Example 1

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:

Matlab Unique Example 2

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:

Matlab Unique Example 3

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:

Input Array Example 4

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:

Input Array Example 5

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 –

  1. What is Switch Statement in Matlab?
  2. Plot Vector Matlab
  3. Matlab sphere() | Syntax and Examples
  4. Examples of Matlab Figure

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW