function [max_min_return] = max_min_calcs(distances, discharges,...
    mass_cell, momentum_cell)
% Function performs the maximum and minimum calculations for discharge,
% mass, and momentum flux

lb_xydist(:,1:2) = distances{1,2};
rb_xydist(:,1:2) = distances{1,3};
lb_dist = distances{1,8};
rb_dist = distances{1,9};
lb_xydist(:,3) = lb_dist(:,6);
rb_xydist(:,3) = rb_dist(:,6);

lb_Q_above_base = discharges{1,6};
rb_Q_above_base = discharges{1,7};

lb_Ma_above_base_per_l = mass_cell{1,6};
rb_Ma_above_base_per_l = mass_cell{1,7};

lb_Mo_above_base_per_l = momentum_cell{1,8};
rb_Mo_above_base_per_l = momentum_cell{1,9};

%% Extract maximum registered discharge
[lb_maxQ,lb_maxQ_I] = ...
    max(lb_Q_above_base,[],2); % left maximum values and index
[rb_maxQ,rb_maxQ_I] = ...
    max(rb_Q_above_base,[],2); % right maximum values and index
[lb_minQ,lb_minQ_I] = ...
    min(lb_Q_above_base,[],2); % left minimum values and index
[rb_minQ,rb_minQ_I] = ...
    min(rb_Q_above_base,[],2); % right minimum values and index

% Combine max/min value with max/min index
lb_maxQ_and_I = [lb_maxQ lb_maxQ_I];
rb_maxQ_and_I = [rb_maxQ rb_maxQ_I];
lb_minQ_and_I = [lb_minQ lb_minQ_I];
rb_minQ_and_I = [rb_minQ rb_minQ_I];

% Change maximum and minimum arrays to only include those values which have
% a discharge greater than 0.01 m^3/s
lb_maxQ_01 = lb_maxQ(lb_maxQ>0.01,:);
lb_maxQ_I01 = lb_maxQ_I(lb_maxQ>0.01,:);
rb_maxQ_01 = rb_maxQ(rb_maxQ>0.01,:);
rb_maxQ_I01 = rb_maxQ_I(rb_maxQ>0.01,:);
lb_minQ_01 = lb_minQ(lb_minQ<-0.01,:);
lb_minQ_I01 = lb_minQ_I(lb_minQ<-0.01,:);
rb_minQ_01 = rb_minQ(rb_minQ<-0.01,:);
rb_minQ_I01 = rb_minQ_I(rb_minQ<-0.01,:);

% Combine max and min values with x- and y-coordinates
lb_maxQ_01(:,2:4) = lb_xydist(lb_maxQ>0.01,:);
lb_minQ_01(:,2:4) = lb_xydist(lb_minQ<-0.01,:);
rb_maxQ_01(:,2:4) = rb_xydist(rb_maxQ>0.01,:);
rb_minQ_01(:,2:4) = rb_xydist(rb_minQ<-0.01,:);
lb_maxQ_01(:,5) = lb_maxQ_I01(:,1);
rb_maxQ_01(:,5) = rb_maxQ_I01(:,1);
lb_minQ_01(:,5) = lb_minQ_I01(:,1);
rb_minQ_01(:,5) = rb_minQ_I01(:,1);

% Order max and min values according to max/min magnitude
lb_maxQ_01_sort = flipud(sortrows(lb_maxQ_01));
lb_minQ_01_sort = sortrows(lb_minQ_01);
rb_maxQ_01_sort = flipud(sortrows(rb_maxQ_01));
rb_minQ_01_sort = sortrows(rb_minQ_01);

%% Extract maximum/minimum mass
[lb_maxMa,lb_maxMa_I] = ...
    max(lb_Ma_above_base_per_l,[],2); % left maximum values and index
[rb_maxMa,rb_maxMa_I] = ...
    max(rb_Ma_above_base_per_l,[],2); % right maximum values and index
[lb_minMa,lb_minMa_I] = ...
    min(lb_Ma_above_base_per_l,[],2); % left minimum values and index
[rb_minMa,rb_minMa_I] = ...
    min(rb_Ma_above_base_per_l,[],2); % right minimum values and index

% Combine max/min value with max/min index for mass
lb_maxMa_and_I = [lb_maxMa lb_maxMa_I];
rb_maxMa_and_I = [rb_maxMa rb_maxMa_I];
lb_minMa_and_I = [lb_minMa lb_minMa_I];
rb_minMa_and_I = [rb_minMa rb_minMa_I];

% Change maximum and minimum arrays to only include those values which have
% a discharge greater than 0.01 m^3/s
lb_maxMa_01 = lb_maxMa(lb_maxMa>1,:);
lb_maxMa_I01 = lb_maxMa_I(lb_maxMa>1,:);
rb_maxMa_01 = rb_maxMa(rb_maxMa>1,:);
rb_maxMa_I01 = rb_maxMa_I(rb_maxMa>1,:);
lb_minMa_01 = lb_minMa(lb_minMa<-1,:);
lb_minMa_I01 = lb_minMa_I(lb_minMa<-1,:);
rb_minMa_01 = rb_minMa(rb_minMa<-1,:);
rb_minMa_I01 = rb_minMa_I(rb_minMa<-1,:);

% Combine max and min values with x- and y-coordinates
lb_maxMa_01(:,2:4) = lb_xydist(lb_maxMa>1,:);
lb_minMa_01(:,2:4) = lb_xydist(lb_minMa<-1,:);
rb_maxMa_01(:,2:4) = rb_xydist(rb_maxMa>1,:);
rb_minMa_01(:,2:4) = rb_xydist(rb_minMa<-1,:);
lb_maxMa_01(:,5) = lb_maxMa_I01(:,1);
rb_maxMa_01(:,5) = rb_maxMa_I01(:,1);
lb_minMa_01(:,5) = lb_minMa_I01(:,1);
rb_minMa_01(:,5) = rb_minMa_I01(:,1);

% Order max and min values according to max/min magnitude
lb_maxMa_01_sort = flipud(sortrows(lb_maxMa_01));
lb_minMa_01_sort = sortrows(lb_minMa_01);
rb_maxMa_01_sort = flipud(sortrows(rb_maxMa_01));
rb_minMa_01_sort = sortrows(rb_minMa_01);

%% Extract maximum/minimum momentum
[lb_maxMo,lb_maxMo_I] = ...
    max(lb_Mo_above_base_per_l,[],2); % left maximum values and index
[rb_maxMo,rb_maxMo_I] = ...
    max(rb_Mo_above_base_per_l,[],2); % right maximum values and index
[lb_minMo,lb_minMo_I] = ...
    min(lb_Mo_above_base_per_l,[],2); % left minimum values and index
[rb_minMo,rb_minMo_I] = ...
    min(rb_Mo_above_base_per_l,[],2); % right minimum values and index

% Combine max/min value with max/min index for mass
lb_maxMo_and_I = [lb_maxMo lb_maxMo_I];
rb_maxMo_and_I = [rb_maxMo rb_maxMo_I];
lb_minMo_and_I = [lb_minMo lb_minMo_I];
rb_minMo_and_I = [rb_minMo rb_minMo_I];

% Change maximum and minimum arrays to only include those values which have
% a discharge greater than 0.01 m^3/s
lb_maxMo_01 = lb_maxMo(lb_maxMo>1,:);
lb_maxMo_I01 = lb_maxMo_I(lb_maxMo>1,:);
rb_maxMo_01 = rb_maxMo(rb_maxMo>1,:);
rb_maxMo_I01 = rb_maxMo_I(rb_maxMo>1,:);
lb_minMo_01 = lb_minMo(lb_minMo<-1,:);
lb_minMo_I01 = lb_minMo_I(lb_minMo<-1,:);
rb_minMo_01 = rb_minMo(rb_minMo<-1,:);
rb_minMo_I01 = rb_minMo_I(rb_minMo<-1,:);

% Combine max and min values with x- and y-coordinates
lb_maxMo_01(:,2:4) = lb_xydist(lb_maxMo>1,:);
lb_minMo_01(:,2:4) = lb_xydist(lb_minMo<-1,:);
rb_maxMo_01(:,2:4) = rb_xydist(rb_maxMo>1,:);
rb_minMo_01(:,2:4) = rb_xydist(rb_minMo<-1,:);
lb_maxMo_01(:,5) = lb_maxMo_I01(:,1);
rb_maxMo_01(:,5) = rb_maxMo_I01(:,1);
lb_minMo_01(:,5) = lb_minMo_I01(:,1);
rb_minMo_01(:,5) = rb_minMo_I01(:,1);

% Order max and min values according to max/min magnitude
lb_maxMo_01_sort = flipud(sortrows(lb_maxMo_01));
lb_minMo_01_sort = sortrows(lb_minMo_01);
rb_maxMo_01_sort = flipud(sortrows(rb_maxMo_01));
rb_minMo_01_sort = sortrows(rb_minMo_01);

%% Create return cell array
max_min_return = cell(1,36);
max_min_return(1,1) = {lb_maxQ_and_I};
max_min_return(1,2) = {rb_maxQ_and_I};
max_min_return(1,3) = {lb_minQ_and_I};
max_min_return(1,4) = {rb_minQ_and_I};
max_min_return(1,5) = {lb_maxQ_01};
max_min_return(1,6) = {rb_maxQ_01};
max_min_return(1,7) = {lb_minQ_01};
max_min_return(1,8) = {rb_minQ_01};
max_min_return(1,9) = {lb_maxQ_01_sort};
max_min_return(1,10) = {rb_maxQ_01_sort};
max_min_return(1,11) = {lb_minQ_01_sort};
max_min_return(1,12) = {rb_minQ_01_sort};
max_min_return(1,13) = {lb_maxMa_and_I};
max_min_return(1,14) = {rb_maxMa_and_I};
max_min_return(1,15) = {lb_minMa_and_I};
max_min_return(1,16) = {rb_minMa_and_I};
max_min_return(1,17) = {lb_maxMa_01};
max_min_return(1,18) = {rb_maxMa_01};
max_min_return(1,19) = {lb_minMa_01};
max_min_return(1,20) = {rb_minMa_01};
max_min_return(1,21) = {lb_maxMa_01_sort};
max_min_return(1,22) = {rb_maxMa_01_sort};
max_min_return(1,23) = {lb_minMa_01_sort};
max_min_return(1,24) = {rb_minMa_01_sort};
max_min_return(1,25) = {lb_maxMo_and_I};
max_min_return(1,26) = {rb_maxMo_and_I};
max_min_return(1,27) = {lb_minMo_and_I};
max_min_return(1,28) = {rb_minMo_and_I};
max_min_return(1,29) = {lb_maxMo_01};
max_min_return(1,30) = {rb_maxMo_01};
max_min_return(1,31) = {lb_minMo_01};
max_min_return(1,32) = {rb_minMo_01};
max_min_return(1,33) = {lb_maxMo_01_sort};
max_min_return(1,34) = {rb_maxMo_01_sort};
max_min_return(1,35) = {lb_minMo_01_sort};
max_min_return(1,36) = {rb_minMo_01_sort};

end