turn function to recursive

1 次查看(过去 30 天)
amateurintraining
amateurintraining 2017-10-20
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.

回答(1 个)

Walter Roberson
Walter Roberson 2017-10-20

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by