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 ビュー (過去 30 日間)
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'.

採用された回答

Walter Roberson
Walter Roberson 2011 年 4 月 28 日
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 件のコメント
Jeffrey
Jeffrey 2011 年 4 月 28 日
meant sort, not ssort
Jeffrey
Jeffrey 2011 年 4 月 28 日
Little sleep and finals week definitely contributes to missing the little things

サインインしてコメントする。

その他の回答 (1 件)

Jeffrey
Jeffrey 2011 年 4 月 28 日
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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by