How to implement a set in MATLAB?
显示 更早的评论
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?

采纳的回答
更多回答(2 个)
Walter Roberson
2016-6-23
2 个投票
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
2016-6-23
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
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!