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

(20 points) Write a MATLAB Function my)fun that takes as its only inp vals (of l

ID: 3762086 • Letter: #

Question

(20 points) Write a MATLAB Function my)fun that takes as its only inp vals (of length 5) and then sets its output variable to (returns) a number the following criteria: 1: if all the values are the same 2: if the values can be arranged to form a consecutive sequence 3: if three of the values are the same, and the other two are also the first three 4: if exactly three values are the same, and the other two are uniq 5: if not any of the above Sample call(s): CALL 1: my_fun([2 5 3 7 5]) % would evaluate to 5 CALL 2: my_fun( [2 5 2 5 2]) % would evaluate to 3 CALL 3: my_fun( [2 5 3 4 1]) % would evaluate to 1

Explanation / Answer

function rt = my_fun(a)
rt = 0;
a=sort(a);

if a(1)==a(2) && a(2)==a(3) && a(3)==a(4) && a(4)==a(5)
rt=1;

elseif a(2)-a(1)==1 && a(3)-a(2)==1 && a(4)-a(3)==1 && a(5)-a(4)==1
rt=2;

elseif(a(1)==a(2) && a(2)==a(3))
if(a(4)==a(5) && a(4)!=a(3))
rt=3;
elseif(a(4)!=a(3) && a(4)!=a(5))
rt=4;
else
rt=5;
end

elseif(a(2)==a(3) && a(3)==a(4))
if(a(1)==a(5) && a(1)!=a(3))
rt=3;
elseif(a(1)!=a(5) && a(1)!=a(3))
rt=4;
else
rt=5;
end

elseif(a(3)==a(4) && a(4)==a(5))
if(a(1)==a(2) && a(1)!=a(3))
rt=3;
elseif(a(1)!=a(2) && a(1)!=a(3))
rt=4;
else
rt=5;
end

else
rt=5;
end

end