A generic dynamic programming Matlab function | Synapse