Code for 'Reverse a Vector'

v = [1 2 3 4 5];
w = reversal(v)
%-----------------------------------------------------------
function w = reversal(v)
persistent z;
s = size(v);
e = s(2);
z(e) = v(1);
if s(2) == 1
return
end
v = v(2:end);
reversal(v);
w = z;
end
% The problem is that i can't pass the random vector.
% Error says Variable w must be of size [1 9]. It is currently of size [1 15].
% Check where the variable is assigned a value.
% Test failed using v = [ -75 -22 57 13 -34 2 -94 -49 -11]
% Why does it work like this?

1 Comment

Did you solve this one? I am stuck on this too, if you could help me out would be great..

Sign in to comment.

Answers (6)

I have done this one,you may check it ---
function w=reversal(v)
s=length(v);
if s==1
w=v;
else
w(1,1)=v(end);
v=v(1:end-1);
w=[w(1,1),reversal(v)];
end

11 Comments

That looks like it would work, but it could be more compact. For example,
w = v(end);
v = v(1:end-1);
w = [w, reversal(v)];
Which can be made even more compact.
What should be our approach if the input vector is very large? For ex: It contains up to a million terms? (v = 1:1e6)
I know that it can be solved quickly using iterative solutions, but i am not able to understand a recursive approach for reversing extremely large vectors. Any assistance would be highly appreciated. Thank you!
As I mentioned above,
Reverse of A B is reverse of B, followed by reverse of A.
Now suppose you let A be the first half of the current vector, and B be the second half of the current vector.
If you proceed in that manner, dividing the task into half each time, then 1e6 elements will need a recusion depth of only 20.
I have understood it. Thank you so much!
Hello @Walter, i have tried with similar definition to flip a large vector of dgree 5. Doesn't know where am wrong but it doesnt work. Kindly have a look. Thaanks in advance.
k=v(end);
b=v(end-1:-1:ceil(length(v)/2));
k=[k reversal(b)];
s=v(ceil(length(v)/2));
h=v(1:ceil(length(v)/2)-1);
s= [s reversal(h)];
y=[k s];
Hemanth:
There are a number of things that are wrong with that code.
Firstly:
This is a question about using recursion, so to implement it correctly, you need to define a function that calls itself recursively.
Recursive functions mostly have the same general structure:
  1. they start by examining the inputs to determine whether the termination conditions have been met, and if so then return a direct computation of the inputs
  2. they do a computation on a part of the input
  3. they pass a modified version of the input to the function itself recursively (sometimes this is done before step 2)
  4. they combine the result of the recursive call with the calculation on part of the input, and return that
Every recursive function will have a conditional test, needed to determine whether the recursion is finished. A function that is intended to be recursive but which does not have a conditional test, will run forever (or until it crashes because you accessed an element that does not exist)
Secondly:
indexing at end-1:-1:ceil(length(v)/2) is reversing in itself, and that is not supposed to be done directly. You should be extracting parts but not reversing them, and passing the parts to the function recursively to do the reversal of
Thirdly:
You are breaking into three pieces, one of which is the last element, and the other two of which are halves of what is remaining after removing the last element. You should not be doing that. You should be working with exactly two pieces. Those two pieces could be halves, or the two pieces could be the last element and the rest of the elements.
Hello walter, thanks for your reply and detailed explanation. Sorry for the incomplete code before, i have defined function but did not paste it here. As i need to reverse a very large vector, I have divided the vector into two halves,used recursive seperatly and tried to concatenate them at the end. Could you please suggest or modify. Thank you.Here is my function :
function y=reversal(v)
y=[];
if length(v)==1
y=v(1);
else
k=v(1:ceil(length(v)/2));
b=v(ceil(length(v)/2)+1 :end);
b=[b(end) reversal(b(1:end-1))];
k=[k(end) reversal(k(1:end-1))];
y=[b k];
end
end
Too much work. Just
function y=reversal(v)
y=[];
if length(v)==1
y=v(1);
else
k=v(1:ceil(length(v)/2));
b=v(ceil(length(v)/2)+1 :end);
y=[reversal(b) reversal(k)];
end
end
Still too much work, and it does crash for empty input.
function v=reversal(v)
if length(v)>1
i = ceil(length(v)/2);
v = [reversal(v(i+1:end)) reversal(v(1:i))];
end
Hey Walter and Bruno, thanks for the help. I was almost there but confused in the end. Thanks again
@Bruno Luong. I have edited Walter's code, and it takes care of empty input:
function y=reversal(v)
y=[];
if length(v)==1
y=v(1);
elseif length(v) > 1
k=v(1:ceil(length(v)/2));
b=v(ceil(length(v)/2)+1 :end);
y=[reversal(b) reversal(k)];
else
return
end
end

Sign in to comment.

I had no this error with
v = [ -75 -22 57 13 -34 2 -94 -49 -11]
You can also use
w = flip(v)
It is same..

1 Comment

Capulus_love
Capulus_love on 13 Aug 2020
Edited: Capulus_love on 13 Aug 2020
i have no error too... but the answer says there is a problem.
i know the function 'flip' , but it needs to use recursive function so i made it.

Sign in to comment.

Mohamed Eid
Mohamed Eid on 10 Feb 2023
Edited: Mohamed Eid on 14 Feb 2023
This code solves the problem and passes all of test cases.
function v = reversal(v)
len = length(v);
if len > 1
len = fix(len/2);
left_be_right = reversal(v(1:len));
right_be_left = reversal(v(len + 1:end));
v = [right_be_left,left_be_right];
end
end
Walter Roberson
Walter Roberson on 13 Aug 2020
That approach is wrong.
Reverse of A B is reverse of B, followed by reverse of A. When you let either A or B be a scalar then reverse of the scalar is the value itself. Therefore you can code each step with just a single recursive call and appending data.
function v=reversal(w)
if length(w)==1
v=w(1);
else
v=[reversal(w(2:end)) w(1)];
end
end

1 Comment

Your function will fail for empty inputs. The edit below fixed that and makes the function more compact.
function v=reversal(v)
if numel(v)>1
v=[reversal(v(2:end)) v(1)];
end
end

Sign in to comment.

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

Categories

Asked:

on 13 Aug 2020

Answered:

on 17 Dec 2023

Community Treasure Hunt

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

Start Hunting!