I'm having trouble creating a function that would reverse a character array using recursion. For example, I want my function to return 'py' when inputting "yp". My overall code doesn't seem right though.
function array2 = charReversal(array1)
if array1==0
return;
else
array2=chsrReversal(reverse(array1,array1-1));
disp(array2)
end

 채택된 답변

Guillaume
Guillaume 2017년 10월 17일

0 개 추천

I assume this is homework so I'm hesitant even answering the question since the whole purpose of the exercise is to make you think of a recursive algorithm.
It is trivial to reverse a string by recursion: take the first element of the string, put it at the end of the string returned by calling the recursive function with the rest of the string (from character 2 to end). The recursion stops when the function receives only 1 character.

댓글 수: 2

Todd Wilson
Todd Wilson 2017년 10월 17일
Yes this is homework, but I am stuck. Your suggestion is a good one to try and I will use it soon. I'm wondering what I should put for "if array1==....."?
Guillaume
Guillaume 2017년 10월 17일
편집: Guillaume 2017년 10월 17일
There is no if array1 == test required. As I wrote, "The recursion stops when the function receives only 1 character". That is what you have to test for.

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Anuj Kumar
Anuj Kumar 2020년 10월 9일

2 개 추천

instead of using v=[v(end) reversal(1:end-1)] which won't satisfactorily on large arrays,i would recommend this piece of code
function v=reversal(v)
if(length(v)>1)
d=length(v);
x=0;
if mod(d,2)==0
x1=d/2;
x2=x1+1;
m=v(1:x1);
n=v(x2:end);
v=[reversal(n),reversal(m)];
else
x1=fix(d/2);
x2=x1+2;
m=v(1:x1);
mid=(x1+1:x1+1);
n=v(x2:end);
v=[reversal(n),v(mid),reversal(m)];
end
end
end
Saurabh Wani
Saurabh Wani 2020년 8월 17일

1 개 추천

i have try this code and it gives reversal of array elements using recursive method
function w=reversal(v)
s=length(v)
if s==1
w=v
else
w=[v(end) reversal(V(1:s-1))]
end

댓글 수: 2

Hazem Ahmed
Hazem Ahmed 2020년 8월 19일
편집: Walter Roberson 2020년 8월 20일
could you explain what are you doing in this line?
w=[v(end) reversal(V(1:s-1))]
Piyush Gupta
Piyush Gupta 2020년 9월 10일
편집: Piyush Gupta 2020년 9월 10일
its an array, you can declare without comma too.

댓글을 달려면 로그인하십시오.

KSSV
KSSV 2017년 10월 17일

0 개 추천

You can either use fliplr or give indices in reverse.
str = 'py' ;
fliplr(str)
str(end:-1:1)

댓글 수: 4

Todd Wilson
Todd Wilson 2017년 10월 17일
편집: Todd Wilson 2017년 10월 17일
I just tried your suggestion of using str(end:-1:1) but I get an error when doing charReversal('ab'):
Undefined function or variable 'str'.
Error in charReversal (line 6) array2=charReversal((str(array1(end:-1:1))));
My code is now:
function [ array2 ] = charReversal( array1 )
if size(array1,2)==1
array2=1;
else
array2=charReversal((str(array1(end:-1:1))));
end
Oh dear friend.... str is the string which should be input...in your case it is array1. Your function should be simply:
function array2 = charReversal(array1)
array2=array1(end:-1:1) ;
Save it, it will be saved on the name charReversal.m. To call/ use it:
array2 = charReversal('py')
array1 = 'matlab' ;
array2 = charReversal(array1)
Todd Wilson
Todd Wilson 2017년 10월 17일
For array2 = charReversal(array1), shouldn't there be a function such as reverse? Also, I need the code to be in recursion.
KSSV
KSSV 2017년 10월 17일
Ohh..why recursion needed? That case you may add it. But don't use str.

댓글을 달려면 로그인하십시오.

ambuj aman
ambuj aman 2020년 9월 15일

0 개 추천

function out = reversal(in)
if length(in) <= 1
out = in;
else
out = [ reversal(in(2:end)) in(1) ];
end
end
Selman Baysal
Selman Baysal 2022년 1월 8일

0 개 추천

I solve this problem by removing first and last elements in the vector until one or zero element remains.
function w = reversal(v)
if length(v) <= 1
w = v;
else
v1 = v(end); v(end) = [];
v2 = v(1); v(1) = [];
w = [v1 reversal(v) v2];
end

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2017년 10월 17일

답변:

2022년 1월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by