Thanks! This has been very useful on a number of projects!
One problem - the function does not handle an empty input array of [2 x 0] and crashes MATLAB. Adding a check of the input dimensionality with a warning if the input is empty and perhaps returning an empty array would be nice.