%% Create Indexed Inequalities in Loop
% Create indexed inequalities and variables to represent the calories consumed in a diet. Each meal has a different calorie limit. Create arrays representing the meals, foods, and calories for each food.

meals = ["breakfast","lunch","dinner"];
foods = ["cereal","oatmeal","yogurt","peanut butter sandwich","pizza","hamburger",...
    "salad","steak","casserole","ice cream"];
calories = [200,175,150,450,350,800,150,650,350,300]';

% Create optimization variables representing the foods for each meal, indexed by food names and meal names.

diet = optimvar('diet',foods,meals,'LowerBound',0);

% Set the inequality constraints that each meal has an upper bound on the calories in the meal.

constr = optimineq(meals);
for i = 1:3
    constr(i) = diet(:,i)'*calories <= 250*i;
end

% View the inequalities for dinner.

show(constr("dinner"))

%
% Instead of using a loop, you can create the same inequalities by using matrix operations on the variables.

constr2 = diet'*calories <= 250*(1:3)';

% Include the appropriate index names for the inequalities by setting the IndexNames property.

constr2.IndexNames = {meals, {}};

% Display the new inequalities for dinner. Note that they are the same as the previous inequalities.

show(constr2("dinner"))

% Creating inequalities in a loop can be more time consuming than creating inequalities by using matrix operations.
%
% Copyright 2019 The MathWorks, Inc.