Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING Matlab I will appreciate if you show your commands and outcome from the pr

ID: 3812015 • Letter: U

Question

USING Matlab I will appreciate if you show your commands and outcome from the program Write a user-defined function that sorts the elements of a ector from the largest to the smallest For the function name and arguments, use y downsort (x). The put to the function is a vector x of any length, and the output y is a vector in which the elements of x are arranged in a descending order DO NOT use the MATLAB built-in functions sor max or min. Test your function on a vector with 14 numbers (integers) randomly distributed randi function to generate the initial vector.

Explanation / Answer

function y = downsort(x)
for j = 2:length(x)
key = x(j)
i = j-1
while i > 0 && x(i) < key
x(i+1) = x(i)
i = i-1
end
x(i+1) = key
end
y = x
end

x = zeros(14,1)

for i = 1:14
x(i) = randi(60) - 30 % get a uniformly distributed random number from -30 to 30
end
y = downsort(x)
disp(y)