How can I define a set
in Matlab which has these properties:
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++?
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With