If you are having Matlab related questions it is always helpful to search for the algorithm you need and to add matlab central to the search request. On Matlab Central you find a wide variety of functions provided by Matlab users. Probably, you ould find the following helpful: