Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of C++ std::set in Matlab

Tags:

c++

matlab

How can I define a set in Matlab which has these properties:

  • Unique items
  • Efficient search
  • Ordered

Maybe there isn't a built-in container, So how can I combine some stuff to gain above things as same as std::set in C++?

like image 949
masoud Avatar asked Jan 14 '13 10:01

masoud


1 Answers

You can use Java's HashSet like this:

>> x = java.util.HashSet;
>> x.add(1);
>> x.add(2);
>> x.contains(1)
ans = 
     1
>> x.contains(3)
ans = 
     0
>> x
x = 
[2.0, 1.0]

In the comments it was pointed out that a HashSet isn't ordered. Which is totally true. My mistake! You could use a TreeSet instead, which is ordered:

>> x = java.util.TreeSet;
>> x.add(1);
>> x.add(3);
>> x.add(2);
>> x
x = 
[1.0, 2.0, 3.0]
like image 103
Chris Taylor Avatar answered Oct 18 '22 01:10

Chris Taylor