Basic While Loop Help?

(Ajay deleted the question, so I (MF) am restoring it.)
I have to write a function that will sort a one-dimensional array using looping and conditionals without using the sort or sortrow commands. The function has to be called MySort.m, which takes as an input a 1-dimensional vector and sorts the values in a speci ed order(ascending or descending). If no order is speci ed, the default should be to sort in ascending order.
I know the function declaration is function output = MySort(input, method) but i don't really know where to go from there?
The second input to the MySort, method, should be a string 'ascending' or 'descending'. If the function is called with only one argument, i.e. output MySort(input), then the data in input should be sorted in ascending order.
Our teacher gave us the hint to use the variable nargin inside MySort and consider making use of the min and max functions and for or while when sorting....but i still need help even starting! t

4 Commenti

Walter Roberson
Walter Roberson il 8 Ott 2012
Please note that tags here do not start with #.
Walter Roberson
Walter Roberson il 8 Ott 2012
Content of the question was edited out of existence by the original poster :(
Image Analyst
Image Analyst il 8 Ott 2012
Modificato: Image Analyst il 8 Ott 2012
For future reference, once someone responds to your post, you can't delete it anymore because there are responses from others now in the thread that don't "belong" to you and therefore you can't delete the entire thread (conversation). So you might want to make sure you've really tried to solve it yourself and used resources like the help facility within MATLAB, and searching the MATLAB Central web site, and the Internet before posting here.
Matt Fig
Matt Fig il 9 Ott 2012
Saved from the Google cache:
I have to write a function that will sort a one-dimensional array using looping and conditionals without using the sort or sortrow commands. The function has to be called MySort.m, which takes as an input a 1-dimensional vector and sorts the values in a speci ed order(ascending or descending). If no order is speci ed, the default should be to sort in ascending order.
I know the function declaration is function output = MySort(input, method) but i don't really know where to go from there?
The second input to the MySort, method, should be a string 'ascending' or 'descending'. If the function is called with only one argument, i.e. output MySort(input), then the data in input should be sorted in ascending order.
Our teacher gave us the hint to use the variable nargin inside MySort and consider making use of the min and max functions and for or while when sorting....but i still need help even starting! t

Accedi per commentare.

Risposte (1)

Andrei Bobrov
Andrei Bobrov il 8 Ott 2012

0 voti

Please see answer by Jan Simon

4 Commenti

Ajay
Ajay il 8 Ott 2012
but where does nargin come into play?
Walter Roberson
Walter Roberson il 8 Ott 2012
if nargin is 1 then the second argument has not been provided.
eg
function [out,idx] = YourSort(A,s)
if nargin < 2, s = 'ascend'; end
...
Walter Roberson
Walter Roberson il 8 Ott 2012
You might also want to test nargin == 0 (that is, no arguments provided)

Accedi per commentare.

Categorie

Richiesto:

il 8 Ott 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by