In Matlab please. I have looked at some of the help for this question already po
ID: 3830980 • Letter: I
Question
In Matlab please. I have looked at some of the help for this question already posted, but continue to get error messages when starting.
Write a function that accepts a variable number of input values and returns the sorted values as an array. The function will be called, for example, like this: b msort (2,1,4,3) and will return a vector b 1 2 3 4] (either row or column vector is OK) The function should have the following features: (10 pts) Help text, including HI (first line) (30 pts) The function should sort in ascending order, i.e., from lowest to highest. (20 pts) Optionally include a string parameter 'd' or 'D to change the sort order to descending, i e, highest to lowest. It should be placed as the last argument to the function, i-e., barmsort(2,1,4,3, 'd') returns 43 21] (20 pts) Have an optional second return argument that includes the number of swaps made during the sort process. If no sorting is done, the number of swaps will be zero. The function is then called as [b,n] msort(2,1,4,3); here b is assigned the sorted value as above, and n is the number of swaps that occurred during the sorting process. (10 pts) Should work for only a single input argument, ie, msort(3) returns 3, rather than generating an error. (10 pts) Issue an error if anything other than a number or 'd' or 'D is sent as an argument Useful functions: returns lower case of a string or character: lower(D) d' Power 0 isnumeric returns TRUE if argument is a number, else returns FALSE (0) ischar0 Run your function on the following lists and include both the final sort value and the number of swaps a) (0 pts) 1, 2, 3,4,5, 6,7,8,9,10 both ascending and descending by pts) Your ID number, using each digit as a separate argument (9 total), ie 106-321 987 would be called as msort(1 0,6,3,2 19,8,7) c) (20 pts) Call the function with ten random numbers. You can either make them up compute them ahead of time and copy them in, or generate them on the fly as follows: msort (rand0,ran rand0 rand0rand0 rand0, rand (),rand0,ranExplanation / Answer
function [elements, nswaps] = msort(varargin)
sorder = 'a';
elements = [];
if ischar(varargin{1,nargin}) == 1
ipSize = nargin - 1;
sorder = lower(varargin{1,nargin});
else
ipSize = nargin;
endif
elements = cell2mat(varargin(1:ipSize));
for i = 1:ipSize
if isnumeric(elements(i)) == 0
fprintf(" Array elements should be numeric ");
return;
endif
endfor
nswaps = 0;
if sorder == 'a'
for i=1:ipSize
for j=2:ipSize - i + 1
if elements(j-1) > elements(j)
temp = elements(j);
elements(j) = elements(j-1);
elements(j-1) = temp;
nswaps = nswaps + 1;
endif
endfor
endfor
elseif sorder == 'd'
for i=1:ipSize
for j=2:ipSize - i + 1
if elements(j-1) < elements(j)
temp = elements(j);
elements(j) = elements(j-1);
elements(j-1) = temp;
nswaps = nswaps + 1;
endif
endfor
endfor
else
fprintf(" Invalid sorting option.... ");
endif
endfunction
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.