# varindex

Map problem variables to solver-based variable index

## Description

returns the linear indices of problem variables as a structure or an integer vector. If you
convert `idx`

= varindex(`prob`

)`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`

.

## Examples

### Obtain Problem Indices

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

### Solve Problem Using Both Approaches

This example shows how to obtain most of the same information using either the problem-based approach or the solver-based approach. First create a problem and solve it using the problem based approach.

x = optimvar('x',3,1,'LowerBound',1,'UpperBound',1); y = optimvar('y',3,3,'LowerBound',-1,'UpperBound',1); prob = optimproblem('Objective',x'*y*x + [2 3 4]*x); rng default x0.x = rand(3, 1); x0.y = rand(3, 3); [solp,fvalp,exitflagp,outputp] = solve(prob,x0);

Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.

Next, convert the problem to solver-based form using `prob2struct`

. To have the `fmincon`

solver use the automatic gradients in the problem, set the `SpecifyObjectiveGradient`

option to `true`

.

```
solverprob = prob2struct(prob,x0);
solverprob.options = optimoptions(solverprob.options,"SpecifyObjectiveGradient",true);
```

Solve the problem using `fmincon`

.

[sols,fvals,exitflags,outputs] = fmincon(solverprob);

Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.

To convert the `fmincon`

solution to the structure form returned by `solve`

, create appropriate structures using `varindex`

.

idx = varindex(prob); sol.x = sols(idx.x); sol.y = sols(idx.y);

The `y`

index that `varindex`

uses is a linear index. Reshape the variable `sol.y`

to have the size of `x0.y`

.

sol.y = reshape(sol.y,size(x0.y));

Check that the two solution structures are identical.

isequal(sol,solp)

`ans = `*logical*
1

The reason that the two approaches are not completely equivalent is that `fmincon`

can return more arguments such as Lagrange multipliers, whereas `solve`

cannot.

## Input Arguments

`prob`

— Optimization problem or equation problem

`OptimizationProblem`

object | `EquationProblem`

object

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;`

`varname`

— Variable name

character vector | string

Variable name, specified as a character vector or string.

**Example: **`'x'`

**Data Types: **`char`

| `string`

## Output Arguments

`idx`

— Linear indices of problem variables

structure | integer vector

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.

## Version History

**Introduced in R2019a**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)