Note: designed to work in Matlab R2011a and later versions.
Earlier versions may require deleting or modifying the line rng('shuffle'), which only introduces some randomness in order to make the code more robust.