sorting an array in ascending, or descending order, using a switch statement, getting Undefined function or method 'ssort' for input arguments of type 'double'.

7 visualizzazioni (ultimi 30 giorni)
Hello all! I'm stuck on this problem. Any insight would be greatly appreciated!
Here's what I'm trying to do. I'm trying to sort an array in ascending order, or descending order, depending on what the user chooses. But the problem is, I can't even get it to sort in ascending order, and I'm not even sure how to even start on getting it to sort in descending order.
I get the error, Undefined function or method 'ssort' for input arguments of type 'double'.
Here's my code:
sortorder = input('Enter the word "up" if you want to sort array in ascending order, \nor enter the word "down" if you want to sort the array in descending order: ', 's');
nvals = input('Enter number of values to sort: ');
switch sortorder
case {'up', 'Up'}
array = zeros(1,nvals);
for ii = 1:nvals
string = ['Enter value ' int2str(ii) ': '];
array(ii) = input(string);
end
%Now sort the data
sorted = ssort(array);
%Display the sorted result
fprintf('\nSorted date:\n');
for ii = 1:nvals
fprintf(' %8.4f\n',sorted(ii));
end
case {'down', 'Down'}
% I STILL NEED TO FIGURE THIS PART OF THE CODE OUT ALSO
end
Oh, and here's an example run of the program
Enter the word "up" if you want to sort array in ascending order,
or enter the word "down" if you want to sort the array in descending order: up
Enter number of values to sort: 5
Enter value 1: 1
Enter value 2: 2
Enter value 3: 3
Enter value 4: 57
Enter value 5: 7
??? Undefined function or method 'ssort' for input arguments of type 'double'.

Risposta accettata

Walter Roberson
Walter Roberson il 28 Apr 2011
Is ssort() intended to be a routine you define? It is not a MATLAB routine. There is a MATLAB routine named sort() (with a single s), but it is obvious from the wording of the assignment that you are intended to write your own sorting routine.
  4 Commenti

Accedi per commentare.

Più risposte (1)

Jeffrey
Jeffrey il 28 Apr 2011
Is there a built in function the sorts the array in descending order? Or will I have to write my own routine for this? Thanks!
Jeff

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by