Write a function with the following header: function [reversed] = my_reverse_wit
ID: 3792552 • Letter: W
Question
Write a function with the following header: function [reversed] = my_reverse_with_recursion(vector) This function should have the same functionality as my_reverse_without_recursion, but you must implement my_reverse_with_recursion using a recursive algorithm. You may not use Matlab's built-in functions flip, fliplr, and flipud in this question. Test cases: >> reversed_char = my_reverse_with_recursion('Hello E7!') reversed_char = !7E olleH >> reversed logical = my_reverse_with_recursion![true; true; false; true)) reversed_logical = 4 times 1 logical array 1 0 1 1 >> reversed_double = my_reverse_with_recursion!(0 : 2 : 10) reversed_double = 10 8 6 4 2 0
Explanation / Answer
Matlab code:
function ans = reverse(l,out)
if(size(l,2) == 0)
ans = out;
else
out = [out, (l(size(l,2)))];
ans = reverse(l(1:size(l,2)-1),out);
end
end
sample output:
>> reverse(['g','d','h'],[])
ans =
hdg
>> reverse(['g','d','h','x','y','z'],[])
ans =
zyxhdg
>> reverse([1,2,3],[])
ans =
3 2 1
>> reverse([1,2,3,1],[])
ans =
1 3 2 1
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.