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!