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

LAB ACTIVITY 17.5.1: Lab 8 Part 5 (Question from Spring 2018 Final Exam) This to

ID: 3199566 • Letter: L

Question

LAB ACTIVITY 17.5.1: Lab 8 Part 5 (Question from Spring 2018 Final Exam) This tol la prowided by a thid party. Though your activiy may be recorded, a page refresh may be needed to fill he banner Two Largest Write a function name twolargest that takes in a vector v and returns, as two seperate outputs, the two largest values in the vector with the first output being the larger of the two. If the largest value occurs more than once, it should just return that value for both outputs. You may not use the built in functions max, min, sort or equivalent built in functions in your solution. Hint: Find the largest, then delete that value from the vector, then find the largest again. Your Function Reset m MATLAB Documentation

Explanation / Answer

function [a, b] = twoLargest(v)
n = length(v);
first = v(1);
first_index = 1;

for i = 1:n %Finding Largest
if v(i) >= first
first = v(i);
first_index = i;
end
end
a= first;

v(first_index) = []; %deleting first largest number

second = v(1);

for i = 1:n-1 %Finding second Largest.
if v(i) >= second
second= v(i);
end

end

b = second;

end