Recursion revisited - can you help me?

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 = [double(v(ii+1:end)) , double(v(1:ii))];
end
end
Walter Roberson
Walter Roberson 2021 年 6 月 28 日
Your second function is not recursive. Also it converts to double for no apparent reason.
Gerry Dumlao
Gerry Dumlao 2021 年 6 月 28 日
i will change it or no?
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 日

0 投票

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 件のコメント

Walter Roberson
Walter Roberson 2022 年 7 月 2 日
What should happen if length(v) is not 1? Currently the code returns v unchanged in that case.
If length(v)==1 is true, then you execute the body of the if, and take length(v) which we know must be 1 (otherwise the condition would have been false.) round(1/2) is 1, so ii will always be 1 in that case (no point in computing it.) So you would call reversal(vv(2:end)) which would be reversal([]) which would end up returning the [] unchanged. And you would call reversal(v(1:1)) which would pass the same v into reversal (v must be scalar or the if would not be true), so you would end up calling reversal() with exactly the same input... which is going to call reversal([]) and reversal(v(1)) again which is going to call reversal([]) and reversal(v(1)) again and...
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 日

0 投票

code run without output

1 件のコメント

Walter Roberson
Walter Roberson 2024 年 3 月 4 日
what code runs without output ?

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2021 年 6 月 27 日

コメント済み:

2024 年 3 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by