function [Y,U,DU,slack,vseq] = mpcCustomFcnMap(z,xQP,old_u,vseq,uscale,yscale,vscale,uoff,yoff,voff,p,m,nu,ny,I2Jm,SuJm,Hv,Sx,Su1,degrees)
% Generate Y,U,DU,X sequences from optimization vector z.

% Author:  A. Bemporad
% Copyright 2017 The MathWorks, Inc.

%% If a constrained problem, split z and slack variables, otherwise, slack is 0.
% m is scalar control horizon (block moves not supported)
if degrees>m*nu
    slack = z(end);
    z = z(1:end-1);
else
    slack = 0;
end
%% Compute offset-free sequences
% DU
DU = zeros(p,nu);
DU(1:m,:) = reshape(z,nu,m)'.*(ones(m,1)*uscale');
% U
U = I2Jm*z;
U = reshape(U,nu,p)'+ones(p,1)*old_u';
% Y
Y = SuJm*z + Sx*xQP + Su1*old_u + Hv*reshape(vseq',numel(vseq),1);
Y = reshape(Y,ny,p)';
%% Add offsets and remove scaling
% U
U = (U+ones(p,1)*(uoff')).*(ones(p,1)*uscale');
% Y
Y = (Y+ones(p,1)*(yoff')).*(ones(p,1)*yscale');
% VSEQ
vseq(:,1:end-1)=(vseq(:,1:end-1)+ones(p+1,1)*(voff')).*(ones(p+1,1)*vscale');
