findindex
Find numeric index equivalents of named index variables
Syntax
Description
Examples
Find Numeric Equivalents of Named Index Variables
Create an optimization variable named colors
that is indexed by the primary additive color names and the primary subtractive color names. Include 'black'
and 'white'
as additive color names and 'black'
as a subtractive color name.
colors = optimvar('colors',["black","white","red","green","blue"],["cyan","magenta","yellow","black"]);
Find the index numbers for the additive colors 'red'
and 'black'
and for the subtractive color 'black'
.
[idxadd,idxsub] = findindex(colors,{'red','black'},{'black'})
idxadd = 1×2
3 1
idxsub = 4
Find Linear Index Equivalents of Named Index Variables
Create an optimization variable named colors
that is indexed by the primary additive color names and the primary subtractive color names. Include 'black'
and 'white'
as additive color names and 'black'
as a subtractive color name.
colors = optimvar('colors',["black","white","red","green","blue"],["cyan","magenta","yellow","black"]);
Find the linear index equivalents to the combinations ["white","black"]
, ["red","cyan"]
, ["green","magenta"]
, and ["blue","yellow"]
.
idx = findindex(colors,["white","red","green","blue"],["black","cyan","magenta","yellow"])
idx = 1×4
17 3 9 15
View Solution with Index Variables
Create and solve an optimization problem using named index variables. The problem is to maximize the profit-weighted flow of fruit to various airports, subject to constraints on the weighted flows.
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. Running HiGHS 1.7.0: Copyright (c) 2024 HiGHS under MIT licence terms Coefficient ranges: Matrix [4e-02, 1e+00] Cost [1e-01, 1e+00] Bound [0e+00, 0e+00] RHS [1e+01, 4e+01] Presolving model 3 rows, 12 cols, 12 nonzeros 0s 3 rows, 12 cols, 12 nonzeros 0s Solving MIP model with: 3 rows 12 cols (0 binary, 12 integer, 0 implied int., 0 continuous) 12 nonzeros Nodes | B&B Tree | Objective Bounds | Dynamic Constraints | Work Proc. InQueue | Leaves Expl. | BestBound BestSol Gap | Cuts InLp Confl. | LpIters Time 0 0 0 0.00% 1160.150059 -inf inf 0 0 0 0 0.0s S 0 0 0 0.00% 1160.150059 1027.233133 12.94% 0 0 0 0 0.0s Solving report Status Optimal Primal bound 1027.23313332 Dual bound 1027.23313332 Gap 0% (tolerance: 0.01%) Solution status feasible 1027.23313332 (objective) 0 (bound viol.) 0 (int. viol.) 0 (row viol.) Timing 0.01 (total) 0.00 (presolve) 0.00 (postsolve) Nodes 1 LP iterations 3 (total) 0 (strong br.) 0 (separation) 0 (heuristics) Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 1e-06. The intcon variables are integer within tolerance, options.ConstraintTolerance = 1e-06.
Find the optimal flow of oranges and berries to New York and Los Angeles.
[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
70 0
This display means that no oranges are going to NYC
, 70 berries are going to NYC
, 980 oranges are going to LAX
, and no berries are going to LAX
.
List the optimal flow of the following:
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 28 980
This display means that 70 berries are going to NYC
, 28 apples are going to BOS
, and 980 oranges are going to LAX
.
Create Initial Point with Named Index Variables
Create named index variables for a problem with various land types, potential crops, and plowing methods.
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);
Set the initial point to a zero array of the correct size.
x0.xcrop = zeros(size(xcrop));
Set the initial value to 3000 for the "wheat-onion"
and "wheat-lentil"
crops that are planted in any dry condition and are plowed traditionally.
[idxLand, idxCrop, idxPlough] = findindex(xcrop, ["dry-good","dry-poor"], ... ["wheat-onion","wheat-lentil"],"tradition"); x0.xcrop(idxLand,idxCrop,idxPlough) = 3000;
Set the initial values for the following three points.
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];
Input Arguments
var
— Optimization variable
OptimizationVariable
object
Optimization variable, specified as an OptimizationVariable
object. Create var
using optimvar
.
Example: var = optimvar('var',4,6)
strindex
— Named index
cell array of character vectors | character vector | string vector | integer vector
Named index, specified as a cell array of character vectors, character
vector, string vector, or integer vector. The number of
strindex
arguments must be the number of dimensions
in var
.
Example: ["small","medium","large"]
Data Types: double
| char
| string
| cell
Output Arguments
numindex
— Numeric index equivalent
integer vector
Numeric index equivalent, returned as an integer vector. The number of output arguments must be one of the following:
The number of dimensions in
var
. Each output vectornumindexj
is the numeric equivalent of the corresponding input argumentstrindexj
.One. In this case, the size of each input
strindex
j
must be the same for allj
, and the output satisfies the linear indexing criterionvar(numindex(j)) = var(strindex1(j),...,strindexk(j))
for allj
.
Version History
Introduced in R2018a
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
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)