Write a function with the following header function reversed


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

Solution

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

 Write a function with the following header: function [reversed] = my_reverse_with_recursion(vector) This function should have the same functionality as my_reve

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site