turn function to recursive
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
See Also
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!