%% Create Named Indices
% Optimization variables can use names for indexing elements. You can give the names when you create a variable or afterward. For example, give the names while creating the variable.

x = optimvar('x',["United","Lufthansa","Virgin Air"])

% optimvar automatically maps the names you specify to index numbers in the order of your variables. For example, "United" corresponds to index 1, "Lufthansa" corresponds to index 2, and "Virgin Air" corresponds to index 3. Display this last variable for confirmation.

show(x(3))

% Index names enable you to address elements of x by the index names. For example:

route = 2*x("United") + 3*x("Virgin Air")

% You can create or change the index names after you create a variable. However, you cannot change the size of an optimization variable after construction. So you can change index names only by setting new names that index the same size as the original variable. For example:

x = optimvar('x',3,2);
x.IndexNames = { {'row1','row2','row3'}, {'col1','col2'} };

% You can set the index names for each dimension individually:

x.IndexNames{1} = {'row1', 'row2', 'row3'};
x.IndexNames{2} = {'col1', 'col2'};

% You can also set an index name for a particular element:

x.IndexNames{1}{2} = 'importantRow';

% Examine the index names for the variable.

x.IndexNames{1}

%

x.IndexNames{2}

% Copyright 2012-2019 The MathWorks, Inc.