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

Cant seem to get this array right in MATLAB, its due at midnight EST 8 October.

ID: 3588998 • Letter: C

Question

Cant seem to get this array right in MATLAB, its due at midnight EST 8 October. Please let me know how to correctly write this code to check with the "Code to Call your Function" part at the bottom! I left what I have in there but its clearly wrong. Change what ever you need, just keep the function part. Thank you!

Array and Conditional Statement 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 nput 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. Your Function Save C Reset EE MATLAB Documentation function out = firstLast(in) out = [ ] L-length(in) if firstLast(1) else 4 5 out-in (1) out [in(1)in(1)1; 9 end Code to call your function C Reset 1 test1 = firstLast(4) 3 test2 = firstLast ([10 9 8 7 6 5 4 3 2 1])

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(4)
firstLast([10 9 8 7 6 5 4 3 2 1])

Output:

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

10 1

ans =

10 1

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