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: cdtgradient.m
    path of file: ./repos/CDT/cdt
    
    the code of the file until where you have to start completion: function [FX,FY] = cdtgradient(lat,lon,F,varargin)
% cdtgradient calculates the spatial gradient of gridded data equally spaced in geographic
% coordinates. 
% 
%% Syntax 
% 
%  [FX,FY] = cdtgradient(lat,lon,F)
%  [FX,FY] = cdtgradient(lat,lon,F,'km
    
function
gridtype = [sign(diff(lon(2,1:2))) sign(diff(lat(1:2,1))) sign(diff(lat(2,1:2))) sign(diff(lon(1:2,1)))]; 

if isequal(abs(gridtype),[1 1 0 0])
   % This means it's the [lon,lat] type, and we're good to keep going.
   permutedims = false; 
   
elseif isequal(abs(gridtype),[0 0 1 1])
   lat = permute(lat,[2 1 3]); 
   lon = permute(lon,[2 1 3]); 
   F = permute(F,[2 1 3]); 
   permutedims = true; 
else
   error('Unrecognized lat,lon grid type. It should be monotonic, like it was created by meshgrid.') 
end