How to implement a set in MATLAB?
Show older comments
In the algorithm below, you can see that there is a variable, C, initialized to the empty set. How would I go about creating a set in MATLAB? Because of this confusion I am unclear on how to code lines 5 and 7 of the algorithm. Furthermore, I am unclear of how to implement the condition on line 13. What is the best way to implement this logic?

Accepted Answer
More Answers (2)
Walter Roberson
on 23 Jun 2016
2 votes
If you want an actual set, in the sense of an unordered collection in which there can be at most one "copy" of any given value, then you can use a vector together with the union and setdiff operators.
Titus Edelhofer
on 23 Jun 2016
Hi,
if I see it correctly, your "set" is meant to be a set of numbers? In this case you might simply use a vector in MATLAB. And the empty set in this case would be
C = [];
To test if an element of a "set", use the function ismember, so line 13 would read
if ismember(n, C)
Titus Titus
1 Comment
Jonathan Mayers
on 23 Jun 2016
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!