varindex

Map problem variables to solver-based variable index

Description

example

idx = varindex(prob) returns the linear indices of problem variables as a structure or an integer vector. If you convert prob to a problem structure by using prob2struct, idx gives the variable indices in the resulting problem structure that correspond to the variables in prob.

example

idx = varindex(prob,varname) returns the linear indices of elements of varname.

Examples

collapse all

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 prob variables.

idx = varindex(prob);
disp(idx.x)
     1     2     3
disp(idx.y)
     4     5     6     7     8     9    10    11    12

Obtain the y indices only.

idxy = varindex(prob,'y')
idxy = 1×9

     4     5     6     7     8     9    10    11    12

Input Arguments

collapse all

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 eqnproblem.

Warning

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.

Example: prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 = cons1;

Example: prob = eqnproblem; prob.Equations = eqs;

Variable name, specified as a character vector or string.

Example: 'x'

Data Types: char | string

Output Arguments

collapse all

Linear indices of problem variables, returned as a structure or an integer vector. If you convert prob to a problem structure by using prob2struct, idx gives the variable indices in the resulting problem structure that correspond to the variables in prob.

  • 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 problem variable.

  • 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 variable.

See Obtain Problem Indices.

Introduced in R2019a