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

Given a vector vecin generate a new vector vecout that contains the same element

ID: 3790681 • Letter: G

Question

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

Explanation / Answer

% 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote