turn function to recursive

1 view (last 30 days)
amateurintraining
amateurintraining on 20 Oct 2017
Answered: Walter Roberson on 20 Oct 2017
I have a function:
function [ array2 ] = charReversal( array1 )
%CHARREVERSAL reverses a character array using recursion
n=length(array1);
if n>1
array2=[array1(end) array1(2:end-1) array1(1)];
array2=[array2(1) array2(end-1) array2(3:end-1-1) array2(1+1) array2(end)];
array2=[array2(1:2) array2(end-2) array2(4:end-1-1-1) array2(1+1+1) array2(end-1:end)];
else
array2=array1;
end
But I don't know how to turn it into a recursive function. I have tried:
function [ array2 ] = charReversal( array1 )
%CHARREVERSAL reverses a character array using recursion
n=length(array1);
if n>1
array2=charReversal([array1(end) array1(2:end-1) array1(1)]);
else
array2=array1;
end
I am having a little trouble with the idea of recursive functions.

Answers (1)

Walter Roberson
Walter Roberson on 20 Oct 2017

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!