Map problem variables to solver-based variable index
Create an optimization problem.
x = optimvar('x',3); y = optimvar('y',3,3); prob = optimproblem('Objective',x'*y*x);
Convert the problem to a structure.
problem = prob2struct(prob);
Obtain the linear indices in
problem of all
idx = varindex(prob); disp(idx.x)
1 2 3
4 5 6 7 8 9 10 11 12
y indices only.
idxy = varindex(prob,'y')
idxy = 1×9 4 5 6 7 8 9 10 11 12
prob— Optimization problem or equation problem
Optimization problem or equation problem, specified as an
OptimizationProblem object or an
EquationProblem object. Create an optimization problem by using
optimproblem; create an equation problem by using
The problem-based approach does not support complex values in an objective function, nonlinear equalities, or nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result can be incorrect.
prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 =
prob = eqnproblem; prob.Equations = eqs;
varname— Variable name
Variable name, specified as a character vector or string.
idx— Linear indices of problem variables
Linear indices of problem variables, returned as a structure or an integer vector.
If you convert
prob to a problem structure by using
idx gives the variable indices in the resulting problem structure
that correspond to the variables in
When you call
idx = varindex(prob), the returned
idx is a structure. The field names of the structure are the
variable names in
prob. The value for each field is the integer
vector of linear indices to which the variables map in the associated solver-based
When you call
idx = varindex(prob,varname), the returned
idx is the vector of linear indices to which the variable
varname maps in the associated solver-based problem