diff --git a/triangulation.m b/triangulation.m
new file mode 100644
index 0000000000000000000000000000000000000000..ab50c8025887bd53e7540d2be24e87046d634886
--- /dev/null
+++ b/triangulation.m
@@ -0,0 +1,45 @@
+function varargout = triangulation(X,Y,varargin)
+%TRIANGULATION Summary of this function goes here
+%   Detailed explanation goes here
+
+p = inputParser();
+p.addOptional('Z'    , []    , @isnumeric);
+p.addOptional('order', 'none', @ischar);
+p.addOptional('fun'  , @(x) min(x,[],2));
+
+p.parse(varargin{:});
+Z = p.Results.Z;
+order = p.Results.order;
+fun = p.Results.fun;
+
+sz = size(X);
+
+[X_,Y_] = ndgrid(1:(sz(1)-1),1:(sz(2)-1));
+TX = reshape( (X_(:)+[0,1,0,1,0,1])' ,3,[])';
+TY = reshape( (Y_(:)+[0,0,1,1,1,0])' ,3,[])';
+
+T = sub2ind(sz,TX,TY);
+
+switch order
+    case 'none'
+        I = (1:size(T,1))';
+    otherwise
+        Z_ = Z(T);
+        Z_ = fun(Z_);
+        [~,I] = sort(Z_,order);
+        T = T(I,:);
+end
+
+if nargout>0
+    varargout{1} = triangulation(T,X(:),Y(:));
+end
+
+if nargout>1
+    varargout{2} = I;
+end
+
+if nargout>2
+    varargout{3} = triangulation(T,X(:),Y(:),Z(:));
+end
+
+end
\ No newline at end of file