Recursive function to reverse a vector

22 次查看(过去 30 天)
The question is to write a function to reverse a vector so v=[1 2 3] becomes v= [3 2 1]
It must be recursive
So my idea how to write this function is start off with d=1 the last digit of w, the output, is equal to the first digit of v the input. so thats what I wrote. As the function develops d eventually equals the length of the original input symoblising the function has reached an end and it will return w. I just don't know how to keep d growing everytime, as currently with every time I recall the function d will rewrite itself to be 1. I think it has something to do with the base case but I don't know. I'm not looking for the answer on a plate but more like a discussion of someone talking me through it
function [w]=reversal(v)
d=1
e=length(v)
w(end-d+1)=v(d)
if d==e
w=w
else
[w]=reversal(v)
end
end

采纳的回答

John D'Errico
John D'Errico 2020-10-21
Hmm. Suppose you strip off the last digit of the vector on each pass? Can you do that? You already know how to find the last element of a vector. That is just v(end). Your logic would look something like the logic in your digit_sum problem.
function vrev = reversal(v)
if length(v) <= 1
vrev = v;
else
% here, you need to break v into two pieces.
% one of those peieces will be the final element of v.
% The other will be everything else.
% then you can build up the vector, appending
% the last element to the BEGINNING of the result.
% take a shot at it, and I will look back in...
end
end
  3 个评论
John D'Errico
John D'Errico 2020-10-21
Yep. In fact, that looks very much like the code I wrote when I tested my algorithm.
The only thing I would add are semi-colons at the ends of your lines. they get rid of the crapola that overwhelms your command window. So you only need to see output that you really want to see.
I think you are getting the hang of recursion. Or perhaps I should say - you will understand recursion when you understand recursion.
Image Analyst
Image Analyst 2020-10-21
Please Accept the answer to award him reputation points for helping you.

请先登录,再进行评论。

更多回答(2 个)

David Hill
David Hill 2020-10-21
编辑:David Hill 2020-10-21
function w=reversal(v)
if length(v)==1
w= v;
else
w= [v(end),reversal(v(1:end-1))];
end
  3 个评论
David Hill
David Hill 2020-10-21
Just like John explained below. You stop the recursion when the length of the vector is equal to 1, otherwise you keep calling the function and growing the layers of recursion. For example: [1 2 3 4] becomes
w=[4 , reversal([1 2 3])]
w=[3, reversal([1 2])]
w=[2, reversal([1])]
w=1
You go 4 layers into the recursion. When the recursion ends, it all comes together.
w=[4 3 2 1]
katherine keogh
katherine keogh 2020-10-21
Yes Thank you! I just needed a bit of extra explanation but I got there! Thank you for taking the time to answer it

请先登录,再进行评论。


SUTITHI
SUTITHI 2022-11-22
function w=reversal(v) if length(v)==1 w= v; else w= [v(end),reversal(v(1:end-1))]; end

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by