AY 1 by a third party though your activity 175.1 Lab 8 Part 5 (Question from Spr
ID: 3906694 • Letter: A
Question
AY 1 by a third party though your activity 175.1 Lab 8 Part 5 (Question from Spring 2018 Final Exam) recorded, a page refresh may be needed to fill the banner Write a function name twol.argest 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 Save Reset?MATLAB Documentation 1 function [Y,X] twolargest(v) s end 6 end Type here to search F3 F5Explanation / Answer
Matlab Code:
function [Y, X] = twolargest(v)
%find first largest
Y = v(1);
index = 1;
for i = 2:length(v)
if v(i) > Y
Y = v(i);
index = i;
end
end
v(index) = []; %remove largest
%find largest in remaining vector
X = v(1);
for i = 2:length(v)
if v(i) > X
X = v(i);
end
end
end
x = randi([1, 10], 20, 1);
[a, b] = twolargest(x)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.