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: invfast.m
    path of file: ./repos/spinw/external
    
    the code of the file until where you have to start completion: function I = invfast(M,opt)
% fast calculation of the matrix inverse of stacked matrices
%
% I = INVFAST(M,{opt})
%
% The function is vectorised and efficient for very large number of small
% matrices (1x1, 2x2 and 3x3). For larger matrices the code calls the
% built-in inv() function. If opt is 'diag', only the diagonal of the
% inverse matrices are calculated.
%

    
function I = invfast(M,opt)
% fast calculation of the matrix inverse of stacked matrices
%
% I = INVFAST(M,{opt})
%
% The function is vectorised and efficient for very large number of small
% matrices (1x1, 2x2 and 3x3). For larger matrices the code calls the
% built-in inv() function. If opt is 'diag', only the diagonal of the
% inverse matrices are calculated.
%
% Input:
%
% M         Matrix with dimensions of [D,D,N1,N2,...], where D can be
%           1, 2 or 3.
% opt       String, with possile values:
%               'full'  Calculate the inverse matrices, the output matrix
%                       will have the same dimensions as the input matrix.
%                       Default option.
%               'diag'  Calculate only the diagonal elements of the inverse
%                       matrix, the output matrix will have dimensions of
%                       [D,N1,N2,...].
%               'sum'   Calculate only the sum of the elements of the
%                       inverse matrix, the output will have dimensions of
%                       [1,N1,N2,...].
%           Default value is 'full'.
%
% Output:
%
% I         Matrix with dimensions determined by opt.
%

if nargin == 0
    help invfast
    return
end