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: regpt2surf.m
    path of file: ./repos/iso2mesh
    
    the code of the file until where you have to start completion: function [A,b,newpos]=regpt2surf(node,elem,p,pmask,A0,b0,cmask,maxiter)
%  [A,b,newpos]=regpt2surf(node,elem,p,pmask,A0,b0,cmask,maxiter)
%  Perform point cloud registration to a triangular surface
%  (surface can be either triangular or cubic), Gauss-Newton method
%  is used for the calculation
%
%  author: Qianqian Fang <q.fang at neu.edu>
%  date: 12/12/2008
%
% parameters: 
%      node: node coordinate of the surface mesh (nn x 3)
%      elem: element list of the surface mesh (3 columns for 
%            triangular mesh, 4 columns for cubic surface mesh)
%      p: points to be registered, 3 columns for x,y and z respectively
%      pmask: a mask vector with the same length as p, determines the 
%         method to handle the point, if pmask(i)=-1, the point is a free
%         node and can be move by the optimization, if pmask(i)=0, the
%         point is fixed; if pmask(i)=n>0, the distance between p(i,:)
%         and node(n,:) will be part of the object function and be optimized
%      A0: a 3x3 matrix, as the initial guess for the affine A matrix (rotation&scaling)
%      b0: a 3x1 vector, as the initial guess for the affine b vector (translation)
%      cmask: a binary 12x1 vector, determines which element of [A(:);b] will be optimized
%          if cmask(i)=0, the corresponding coefficient will not be updated
%      maxiter: a integer, specifying the optimization iterations
%
% outputs:
%      A: 3x3 matrix, the updated affine A matrix
%      b: 3x1 vector, the updated affine b vector

    
function [A,b,newpos]=regpt2surf(node,elem,p,pmask,A0,b0,cmask,maxiter)
%  [A,b,newpos]=regpt2surf(node,elem,p,pmask,A0,b0,cmask,maxiter)
%  Perform point cloud registration to a triangular surface
%  (surface can be either triangular or cubic), Gauss-Newton method
%  is used for the calculation
%
%  author: Qianqian Fang <q.fang at neu.edu>
%  date: 12/12/2008
%
% parameters: 
%      node: node coordinate of the surface mesh (nn x 3)
%      elem: element list of the surface mesh (3 columns for 
%            triangular mesh, 4 columns for cubic surface mesh)
%      p: points to be registered, 3 columns for x,y and z respectively
%      pmask: a mask vector with the same length as p, determines the 
%         method to handle the point, if pmask(i)=-1, the point is a free
%         node and can be move by the optimization, if pmask(i)=0, the
%         point is fixed; if pmask(i)=n>0, the distance between p(i,:)
%         and node(n,:) will be part of the object function and be optimized
%      A0: a 3x3 matrix, as the initial guess for the affine A matrix (rotation&scaling)
%      b0: a 3x1 vector, as the initial guess for the affine b vector (translation)
%      cmask: a binary 12x1 vector, determines which element of [A(:);b] will be optimized
%          if cmask(i)=0, the corresponding coefficient will not be updated
%      maxiter: a integer, specifying the optimization iterations
%
% outputs:
%      A: 3x3 matrix, the updated affine A matrix
%      b: 3x1 vector, the updated affine b vector
%      newpos: the registered positions for p, newpos=A*p'+b
%
% Please find more information at http://iso2mesh.sf.net/cgi-bin/index.cgi?metch
%
% this function is part of "metch" toobox, see COPYING for license

A=A0;
b=b0(:);

% for simplicity, I wrap A and b into one single vector C
C=[A(:);b];

delta=1e-4;
newpos=(reshape(C(1:9),3,3)*p'+repmat(C(end