How to implement a set in MATLAB?
Mostrar comentarios más antiguos
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?

Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 23 de Jun. de 2016
2 votos
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
el 23 de Jun. de 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 comentario
Jonathan Mayers
el 23 de Jun. de 2016
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!