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: surf_to_mesh.m
    path of file: ./repos/gptoolbox/mesh
    
    the code of the file until where you have to start completion: function [V,F] = surf_to_mesh(X,Y,Z)
  % SURF_TO_MESH Convert a grid-based `surf` representation of surface to a
  % triangle mesh representation.
  %
  % [V,F] = surf_to_mesh(X,Y,Z)
  %
  % Inputs:
  %   X  m by n list of x-coordinates
  %   Y  m by n list of y-coordinates
  %   Z  m by n list of z-coordinates
  % Outputs:

    
function [V,F] = surf_to_mesh(X,Y,Z)
  % SURF_TO_MESH Convert a grid-based `surf` representation of surface to a
  % triangle mesh representation.
  %
  % [V,F] = surf_to_mesh(X,Y,Z)
  %
  % Inputs:
  %   X  m by n list of x-coordinates
  %   Y  m by n list of y-coordinates
  %   Z  m by n list of z-coordinates
  % Outputs:
  %   V  #V by 3 list of vertex positions
  %   F  #F by 3 list of face indices
  %

  clean = true;
  s2p = surf2patch(X,Y,Z);
  F = s2p.faces;
  V = s2p.vertices;
  F = [F(:,[1 2 3]);F(:,[1 3 4])];

  if clean
    [V,VI,VJ] = remove_duplicate_vertices(V,1e-7);
    F = VJ(F);
    F = F(doublearea(V,F)>eps,:);
  end