You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: pmat.m
    path of file: ./repos/spinw/external
    
    the code of the file until where you have to start completion: function Mpad = pmat(M,dims,pVal)
% pads matrix to the required dimensions with a given value
%
% Mpad = PMAT(M,dims,{pVal})
%
% The output M is padded with zeros or the value given by pVal. If dims
% define a smaller dimension than the input matrix, the matrix gets cutted.
%
% Input:
%

    
function Mpad = pmat(M,dims,pVal)
% pads matrix to the required dimensions with a given value
%
% Mpad = PMAT(M,dims,{pVal})
%
% The output M is padded with zeros or the value given by pVal. If dims
% define a smaller dimension than the input matrix, the matrix gets cutted.
%
% Input:
%
% M         Input matrix with arbitrary dimensions.
% dims      Row vector, gives the dimensions of the output matrix.
% pVal      Value that is used for padding, default is 0.
%
% Output:
%
% Mpad      Output matrix with the padded values.
%

% create empty matrix with the padded value
Mpad = zeros([dims 1])+pVal;

% dimensions of the input matrix
dimi = size(M);
if numel(dimi)<numel(dims)
    dimi(end