Given a vector vecin generate a new vector vecout that conta

Given a vector vecin generate a new vector vecout that contains the same elements but ordered from smallest to largest. YOU SHOULD NOT USE THE SORT FUNCTION Instructions: To solve this problem, modify the template bellow with your code. Leave the name of the function and variables unchanged. Otherwise your code will not pass the test. Click Test to test your solution before submitting it. When you are satisfied with it click Submit. Solution 1 |function vecout = ordered(vecin) %Don\'t change this line % Place your code here end

Solution

% matlab code

function vecout = ordered(vecin)
%iterate over the input vector
for i=1:length(vecin)
for j=1:length(vecin)
% swap indexes if value at i is less than j.
if vecin(i) < vecin(j)
temp= vecin(i);
vecin(i) = vecin(j);
vecin(j) = temp;
end
end
end
% assign to output
vecout = vecin;
end

vecout = ordered([1,5,4,3,7]);
disp(vecout);
%output: 1 3 4 5 7

 Given a vector vecin generate a new vector vecout that contains the same elements but ordered from smallest to largest. YOU SHOULD NOT USE THE SORT FUNCTION In

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site