Sets of Objects

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


Set of type DOM_SET can store an arbitrary finite number of arbitrary MuPAD® objects, except for sequences and the null object.

In MuPAD, finite sets are implemented with the data type DOM_SET. Sets are unordered collections of arbitrary objects, with identical objects appearing only once. Sequences (objects separated by commas) are “flattened” when put into a set, i.e., instead of the sequence, its elements are placed into the set. The null object is treated as the empty sequence, i.e., it does not result in an element in the set.

Sets can be empty. The empty set is displayed as .

Function Calls

Using a set as a function symbol creates the set obtained by using each element as a function symbol for the operands used, i.e., {f, g}(x, y) results in {f(x, y), g(x, y)}.


Assuming that S is a set, the number of elements in S can be obtained by calling nops(S).

Individual elements of the set can be obtained in two subtly different ways:

  1. Using an indexed access, as in S[2], returns the n-th element of the set, counted in the order as the set appears on the screen. This is a potentially slow operation, since it requires determining that order for each access, i.e., sorting the set.

    Negative indices are accepted, counting from the end of the sequence of elements. Trying to access an element “outside” the set or S[0] raises an error.

  2. Using op, as in op(S, 2), returns the n-th element of the set, counted in the internal order. This is a fast operation (O(n) to get the n-th element, irrespective of the size of S), but the internal order of two mathematically identical sets can be completely different and almost any operation changing a set can completely change its internal order, so no assumptions should be made.

Both of these ways also accept ranges as indices. S[2..4] returns the set {S[2], S[3], S[4]}, while op(S, 2..4) returns the sequence op(S, 2), op(S, 3), op(S, 4).

To iterate over all elements of a set in no particular order, using map or the $ operator is highly superior to using a for-loop with either of the above element access methods. If a for-loop is required, you should use the form for s in S, which has linear complexity as well.

The usual set operations are provided as infix operations: union, minus, intersect.

To change an element of a set, the preferred method is to remove it using minus and adding a new one using union. It is also possible to replace an element with subsop; replacing an element with null() deletes it from the set. (Note that subsop does not do a side-effect assignment.)

The function contains checks for occurrence of a given MuPAD object in a set; see also the in operator for the same purpose, but with different evaluation semantics. select and split can be used to extract those elements from a set fulfilling an arbitrary predicate.

To get a list of the elements of a set, use coerce. To get such a list with the elements ordered in the same way as printed on the screen, use DOM_SET::sort(S).


The operands of a set are its elements, in the internal order. (See above for details.)


Sets are ordered for the output.

Element Creation

The most direct way of creating a set is to place a sequence of MuPAD objects (separated by commas) between curly brackets, as in {1, 2, 3}.

See Also

MuPAD Domains