function b = squeeze(a)
%MATLAB Code Generation Library Function

%   Copyright 2006-2019 The MathWorks, Inc.
%#codegen

coder.internal.allowEnumInputs;
coder.internal.allowHalfInputs;
coder.internal.assert(nargin == 1,'MATLAB:minrhs');
coder.internal.assert(~iscell(a),'Coder:toolbox:CellArraysNotSupported',mfilename);
ndimsa = coder.const(coder.internal.indexInt(eml_ndims(a)));
ONE = coder.internal.indexInt(1);
ndimsb = coder.const(squeezeNdims(a));
sqsz = cell(1,ndimsb);
for k = ONE:ndimsb
    sqsz{k} = ONE;
end
if isrow(a)
    % Special case: row vectors are not squeezed.
    sqsz{2} = coder.internal.indexInt(size(a,2));
else
    j = coder.internal.indexInt(0);
    for k = coder.unroll(1:ndimsa)
        if coder.const(coder.internal.isConst(size(a,k)) && size(a,k) == 1)
        elseif size(a,k) == 1
        else
            j = j + 1;
            sqsz{j} = coder.internal.indexInt(size(a,k));
        end
    end
end
b = reshape(a,sqsz{:});

%--------------------------------------------------------------------------

function ndimsb = squeezeNdims(a)
singdims = coder.const(numConstSingletonDims(a));
ndimsa = coder.internal.indexInt(eml_ndims(a));
if coder.const(singdims > ndimsa - 2)
    ndimsb = coder.internal.indexInt(2);
else
    ndimsb = ndimsa - singdims;
end

%--------------------------------------------------------------------------

function singdims = numConstSingletonDims(a)
singdims = coder.internal.indexInt(0);
for k = coder.unroll(1:eml_ndims(a))
    if coder.internal.isConst(size(a,k)) && size(a,k) == 1
        singdims = singdims + 1;
    end
end

%--------------------------------------------------------------------------
