Recursion revisited - can you help me?

조회 수: 4 (최근 30일)
Gerry Dumlao
Gerry Dumlao 2021년 6월 27일
댓글: Walter Roberson 2024년 3월 4일
function v = reversal(v)
if length(v) > 1
v = [v(end) reversal(v(1:end-1))];
end
end
  댓글 수: 5
Gerry Dumlao
Gerry Dumlao 2021년 6월 28일
function v = reversal2(v)
if length(v) > 1
ii=round(length(v) /2 );
v = [reversal2(v(ii+1:end)) , reversal2(v(1:ii))];
end
end
is this correct?
Walter Roberson
Walter Roberson 2021년 6월 28일
Tests out okay
V = char(randi([33 126], 1, 31))
V = '*^jMW@uXA#]RGVBM3|7;F?6YoAy"_;['
RV = reversal2(V)
RV = '[;_"yAoY6?F;7|3MBVGR]#AXu@WMj^*'
isequal(RV, fliplr(V))
ans = logical
1
V = char(randi([33 126], 1, 32))
V = 'Q|#`bcXGa;U3(WMtLD2'e;+}mQltHR5g'
RV = reversal2(V)
RV = 'g5RHtlQm}+;e'2DLtMW(3U;aGXcb`#|Q'
isequal(RV, fliplr(V))
ans = logical
1
function v = reversal2(v)
if length(v) > 1
ii=round(length(v) /2 );
v = [reversal2(v(ii+1:end)) , reversal2(v(1:ii))];
end
end

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

답변 (2개)

ghazal
ghazal 2022년 7월 2일
I have problem and this is my code, anyone can help me?
function v=reversal(v)
if length(v)==1
ii=round(length(v)/2);
v=[reversal(v(ii+1:end)) , reversal(v(1:ii))];
end
end
  댓글 수: 3
ghazal
ghazal 2022년 7월 3일
Thanks friend for your explanation actually I don't get where the problem is, but I changed my code to this and I get this Error!
Error:
Undefined function 'reversal' for input arguments of type 'double'.
Code:
function v = reversal2(v)
if length(v) > 1
ii=round(length(v) /2 );
v = [reversal2(v(ii+1:end)) , reversal2(v(1:ii))];
end
end
Walter Roberson
Walter Roberson 2024년 3월 4일
You would have a problem running function reversal when the function is named reversal2

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


Jeevan
Jeevan 2024년 3월 4일
code run without output

카테고리

Help CenterFile Exchange에서 Visualize and Interpret Parallel Link Project Analysis Results에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by