Create Optimization Variables Indexed by Strings
Create an integer optimization variable vector named bolts that is indexed by the strings "brass", "stainless", and "galvanized". Use the indices of bolts to create an optimization expression, and experiment with creating bolts using character arrays or in a different orientation.
Create bolts using strings in a row orientation.
bnames = ["brass","stainless","galvanized"]; bolts = optimvar('bolts',bnames,'Type','integer')
bolts = 1×3 OptimizationVariable array with properties: Array-wide properties: Name: 'bolts' Type: 'integer' IsSubset: 0 IndexNames: {{} {1×3 cell}} Elementwise properties: LowerBound: [-Inf -Inf -Inf] UpperBound: [Inf Inf Inf] See variables with showvar. See bounds with showbounds.
Create an optimization expression using the string indices.
y = bolts("brass") + 2*bolts("stainless") + 4*bolts("galvanized")
y = OptimizationExpression bolts(1, 'brass') + 2*bolts(1, 'stainless') + 4*bolts(1, 'galvanized')
Use a cell array of character vectors instead of strings to get a variable with the same indices as before.
bnames = {'brass','stainless','galvanized'}; bolts = optimvar('bolts',bnames,'Type','integer')
bolts = 1×3 OptimizationVariable array with properties: Array-wide properties: Name: 'bolts' Type: 'integer' IsSubset: 0 IndexNames: {{} {1×3 cell}} Elementwise properties: LowerBound: [-Inf -Inf -Inf] UpperBound: [Inf Inf Inf] See variables with showvar. See bounds with showbounds.
Use a column-oriented version of bnames, 3-by-1 instead of 1-by-3, and observe that bolts has that orientation as well.
bnames = ["brass";"stainless";"galvanized"]; bolts = optimvar('bolts',bnames,'Type','integer')
bolts = 3×1 OptimizationVariable array with properties: Array-wide properties: Name: 'bolts' Type: 'integer' IsSubset: 0 IndexNames: {{1×3 cell} {}} Elementwise properties: LowerBound: [3×1 double] UpperBound: [3×1 double] See variables with showvar. See bounds with showbounds.