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

Write a function that takes an array as an input, and returns an array as an out

ID: 3588941 • Letter: W

Question

Write a function that takes an array as an input, and returns an array as an output. Your function should check to make sure that the array has more than one element. If it does not, it should return an empty array. If the array has more than one element, your output should be an array with two elements. The first element should be the first element of the input array. The second element should be the last element of the input array. Please look up the "length" function in MATLAB to help you with this problem.

Explanation / Answer

function out = firstLast(in)
if length(in) == 0 || length(in) == 1
out = []
else
out(1) = in(1)
out(2) = in(length(in))
end
end

firstLast([1])
firstLast([1,2,3,5])

Output:

octave: X11 DISPLAY environment variable not set
octave: disabling GUI features
out = [](0x0)
ans = [](0x0)
out = 1
out =

1 5

ans =

1 5

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