function z = znlmpc_setDecisions(coredata, X, Umv, e)
% Translate projected states, manipulated variables and slack variable into
% a nonlinear MPC decision vector.
%
% z     Vector of decision variables to be used by nonlinear optimization
%
% X     Projected states, p-by-nx, from k+1 to k+p.  In engineering units
% Umv   Projected MV, p-by-nmv, from k to k+p-1.  In engineering units
% e     Slack variable used for constraint softening, scalar
%
% nx    Number of prediction-model states
% nmv   Number of manipulated plant input channels
% p     Number of steps in the prediction horizon
%
% NOTE: if k is the current time, U(1,:) refers to time k, and X(1,:)
%       refers to time k+1.  The current state is not a decision for 
%       optimization, i.e., it is fixed, typically from state estimation.
%       
%       If X and/or U contain more than p rows, the excess rows will be
%       ignored..  I they include fewer than p rows, the last row will be
%       used to set the unspecified values.

%   Author: N. L. Ricker, R. Chen
%   Copyright 1986-2018 The MathWorks, Inc.

%#codegen
p = coredata.p;
nmv = coredata.nmv;
nx = coredata.nx;
% Scale states
if coredata.hasxscale
    X = znlmpc_scaleRows(X, coredata.Xscale');
end
xz = reshape(X',p*nx,1);
% Scale and map Umv to actual decision variables 
uz = coredata.Iu2z*reshape(Umv',p*nmv,1);
% Create the z vector
z = [xz; uz; e];

