# findindex

## 语法

``[numindex1,numindex2,...,numindexk] = findindex(var,strindex1,strindex2,...,strindexk)``
``numindex = findindex(var,strindex1,strindex2,...,strindexk)``

## 说明

``[numindex1,numindex2,...,numindexk] = findindex(var,strindex1,strindex2,...,strindexk)` 在优化变量 `var` 中查找命名索引变量的等效数值索引。`

``numindex = findindex(var,strindex1,strindex2,...,strindexk)` 查找命名索引变量的等效线性索引。`

## 示例

`colors = optimvar('colors',["black","white","red","green","blue"],["cyan","magenta","yellow","black"]);`

`[idxadd,idxsub] = findindex(colors,{'red','black'},{'black'})`
```idxadd = 1×2 3 1 ```
```idxsub = 4 ```

`colors = optimvar('colors',["black","white","red","green","blue"],["cyan","magenta","yellow","black"]);`

`idx = findindex(colors,["white","red","green","blue"],["black","cyan","magenta","yellow"])`
```idx = 1×4 17 3 9 15 ```

```rng(0) % For reproducibility p = optimproblem('ObjectiveSense', 'maximize'); flow = optimvar('flow', ... {'apples', 'oranges', 'bananas', 'berries'}, {'NYC', 'BOS', 'LAX'}, ... 'LowerBound',0,'Type','integer'); p.Objective = sum(sum(rand(4,3).*flow)); p.Constraints.NYC = rand(1,4)*flow(:,'NYC') <= 10; p.Constraints.BOS = rand(1,4)*flow(:,'BOS') <= 12; p.Constraints.LAX = rand(1,4)*flow(:,'LAX') <= 35; sol = solve(p);```
```Solving problem using intlinprog. LP: Optimal objective value is -1027.472366. Heuristics: Found 1 solution using ZI round. Upper bound is -1027.233133. Relative gap is 0.00%. Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance, options.IntegerTolerance = 1e-05 (the default value). ```

`[idxFruit,idxAirports] = findindex(flow, {'oranges','berries'}, {'NYC', 'LAX'})`
```idxFruit = 1×2 2 4 ```
```idxAirports = 1×2 1 3 ```
`orangeBerries = sol.flow(idxFruit, idxAirports)`
```orangeBerries = 2×2 0 980.0000 70.0000 0 ```

`Fruit Airports`

` ----- --------`

` Berries NYC`

` Apples BOS`

` Oranges LAX`

`idx = findindex(flow, {'berries', 'apples', 'oranges'}, {'NYC', 'BOS', 'LAX'})`
```idx = 1×3 4 5 10 ```
`optimalFlow = sol.flow(idx)`
```optimalFlow = 1×3 70.0000 28.0000 980.0000 ```

```land = ["irr-good","irr-poor","dry-good","dry-poor"]; crops = ["wheat-lentil","wheat-corn","barley-chickpea","barley-lentil","wheat-onion","barley-onion"]; plow = ["tradition","mechanized"]; xcrop = optimvar('xcrop',land,crops,plow,'LowerBound',0);```

`x0.xcrop = zeros(size(xcrop));`

```[idxLand, idxCrop, idxPlough] = findindex(xcrop, ["dry-good","dry-poor"], ... ["wheat-onion","wheat-lentil"],"tradition"); x0.xcrop(idxLand,idxCrop,idxPlough) = 3000;```

```Land Crops Method Value dry-good wheat-corn mechanized 2000 irr-poor barley-onion tradition 5000 irr-good barley-chickpea mechanized 3500 ```
```idx = findindex(xcrop,... ["dry-good","irr-poor","irr-good"],... ["wheat-corn","barley-onion","barley-chickpea"],... ["mechanized","tradition","mechanized"]); x0.xcrop(idx) = [2000,5000,3500];```

## 输出参数

• `var` 中的维数。每个输出向量 `numindexj` 是对应输入参数 `strindexj` 的等效数值索引。

• 1。在这种情况下，对于所有 `j`，每个输入 `strindex``j` 的大小必须相同，并且输出满足线性索引条件

```var(numindex(j)) = var(strindex1(j),...,strindexk(j))```（对于全部 `j`）。