Documentation Center |
Select operands
This functionality does not run in MATLAB.
select(object, f, <p1, p2, …>)
select(object, f) returns a copy of the object with all operands removed that do not satisfy a criterion defined by the procedure f.
select is a fast and handy function for picking out elements of lists, sets, tables etc. that satisfy a criterion set by the procedure f.
The function f must return a value that can be evaluated to one of the Boolean values TRUE, FALSE, or UNKNOWN. It may either return one of these values directly, or it may return an equation or an inequality that can be simplified to one of these values by the function bool.
Internally, the function f is applied to all operands x of the input object via the call f(x, p1, p2, ...). If the result is not TRUE, this operand is removed. The original object is not modified in this process.
The output object is of the same type as the input object, i.e., a list yields a list, a set yields a set etc.
An input object that is an expression sequence is not flattened. Cf. Example 2.
Also "atomic" objects such as numbers or identifiers can be passed to select as first argument. Such objects are handled like sequences with a single operand.
select handles lists and sets. In the first example, we select all true statements from a list of logical statements. The result is again a list:
select([1 = 1, 1 = 2, 2 = 1, 2 = 2], bool)
In the following example, we extract the subset of all elements that are recognized as zero by iszero:
select({0, 1, x, 0.0, 4*x}, iszero)
select also works on tables:
T:= table(1 = "y", 2 = "n", 3 = "n", 4 = "y", 5 = "y"): select(T, has, "y")
The following expression is a sum, i.e., an expression of type "_plus". We extract the sum of all terms that do not contain x:
select(x^5 + 2*x + y - 4, _not@has, x)
We extract all factors containing x from the following product. The result is a product with exactly one factor, and therefore, is not of the syntactical type "_mult":
select(11*x^2*y*(1 - y), has, x)
delete T:
select works for expression sequences:
select((1, -4, 3, 0, -5, -2), testtype, Type::Negative)
The $ command generates such expression sequences:
select(i $ i = 1..20, isprime)
Atomic objects are treated as expression sequences of length one:
select(5, isprime)
The following result is the void object null() of type DOM_NULL:
domtype(select(6, isprime))
It is possible to pass an "anonymous procedure" to select. This allows to perform more complex actions with one call. In the following example, the command anames(All) returns a set of all identifiers that have a value in the current MuPAD^{®} session. The select statement extracts all identifiers beginning with the letter "h":
select(anames(All), x -> expr2text(x)[1] = "h")
object |
A list, a set, a table, an expression sequence, or an expression of type DOM_EXPR |
f |
A procedure returning a Boolean value |
p1, p2, … |
Any MuPAD objects accepted by f as additional parameters |