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

조회 수: 10 (최근 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일
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 CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by