Perform element-wise conditional operations with array-type predicates
Parent Section: equationsintermediates
You can perform element-wise conditional operations with array-type predicates by
.if statements. These statements are similar to
if statements, but in an
predicates must be scalar. To evaluate array-type predicates, use
The following rules and restrictions apply:
.elseifbranches are optional. Any subsequent
.elseifbranch must have a predicate of the same size and shape as the original
Each branch of the
.if-.elseif-.elsestatement must match the size and shape of the predicates. Scalar expansion applies.
Whether scalar expansion is necessary is determined on a branch-by-branch basis, so using an array in one branch and a scalar in another is allowed.
Units must be commensurate between branches.
.ifstatement with the
Output is an array of the same size and shape as the
.ifbranch predicate. For multiple return values, each output variable is the same size and shape as the predicate.
Array-type predicates are not supported for:
Perform Conditional Assignment to an Array-Type Intermediate
Perform conditional assignment to an intermediate based on array-type predicates:
component my_comp inputs pred1 = [1 0 0]; pred2 = [0 0 1]; end parameters A = [1 2 3]; B = [4 5 6]; C = [7 8 9]; end intermediates y = .if pred1, A; .elseif pred2, B; .else C; .end end ... % Other parameters, variables, equations end
With these input values, the intermediate
[1 8 6] because:
The first element of
pred1is true, so the first value is the first element of parameter
The second element of neither
pred2is true, so the second value is the second element of parameter
The third element of
pred2is true, so the third value is the third element of parameter
Vectorized Saturation Block
This component represents a vectorized saturation block:
component vec_saturation inputs I = zeros([3 3]); end outputs ITrue = zeros(size(I)); end parameters upper_limit = 5; lower_limit = 0.7; end equations ITrue == .if I>=upper_limit, upper_limit; .elseif I <= lower_limit, lower_limit; .else I; .end end end
Iis within the bounds specified by the
ITrueis equal to
If the value of
Iis outside the bounds,
ITruesaturates to the upper limit or lower limit, respectively.
Introduced in R2023b