필터 지우기
필터 지우기

Code for 'Reverse a Vector'

조회 수: 30 (최근 30일)
Capulus_love
Capulus_love 2020년 8월 13일
답변: Rajith 2023년 12월 17일
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
Atif Penkar
Atif Penkar 2020년 8월 23일
Did you solve this one? I am stuck on this too, if you could help me out would be great..

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

답변 (6개)

Md Jawarul Moresalein Ayon
Md Jawarul Moresalein Ayon 2020년 8월 28일
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
Hemanth Kumar Reddy Boreddy
Hemanth Kumar Reddy Boreddy 2020년 9월 7일
Hey Walter and Bruno, thanks for the help. I was almost there but confused in the end. Thanks again
Micheal Omojola
Micheal Omojola 2020년 11월 9일
편집: Micheal Omojola 2020년 11월 9일
@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

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


Serhii Tetora
Serhii Tetora 2020년 8월 13일
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
Capulus_love
Capulus_love 2020년 8월 13일
편집: Capulus_love 2020년 8월 13일
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.

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


Mohamed Eid
Mohamed Eid 2023년 2월 10일
편집: Mohamed Eid 2023년 2월 14일
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 2020년 8월 13일
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.

xin yi leow
xin yi leow 2021년 1월 25일
function v=reversal(w)
if length(w)==1
v=w(1);
else
v=[reversal(w(2:end)) w(1)];
end
end
  댓글 수: 1
Rik
Rik 2021년 1월 25일
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

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


Rajith
Rajith 2023년 12월 17일
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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by