%% Use Named Indices
% You can create and debug some problems easily by using named index variables. For example, consider the variable x that is indexed by the names in vars:

vars = {'P1','P2','I1','I2','C','LE1','LE2','HE1','HE2',...
    'HPS','MPS','LPS','BF1','BF2','EP','PP'};
x = optimvar('x',vars,'LowerBound',0);

% Create bounds, an objective function, and linear constraints for x by using the named indices.

x('P1').LowerBound = 2500;
x('I2').UpperBound = 244000;
linprob = optimproblem;
linprob.Objective = 0.002614*x('HPS') + 0.0239*x('PP') + 0.009825*x('EP');
linprob.Constraints.cons1 = x('I1') - x('HE1') <= 132000;

% You can use strings (" ") or character vectors (' ') in index variables indiscriminately. For example:

x("P2").LowerBound = 3000;
x('MPS').LowerBound = 271536;
showbounds(x)

% There is no distinction between variables you specified with a string, such as x("P2"), and variables you specified with a character vector, such as x('MPS').
% Because named index variables have numeric equivalents, you can use ordinary summation and colon operators even when you have named index variable. For example, you can have constraints of these forms:

constr = sum(x) <= 100;
show(constr)

%

y = optimvar('y',{'red','green','blue'},{'plastic','wood','metal'},...
    'Type','integer','LowerBound',0);
constr2 = y("red",:) == [5,7,3];
show(constr2)

% Copyright 2012-2019 The MathWorks, Inc.