how do i reverse a vector

 采纳的回答

hi,
To reverse a vector try the function ' wrev' , here is an example :
r=wrev(1:4)
If you to control the degree of reverse/shifting try 'circshift' function.

2 个评论

I like this solution. Does anybody know how fliplr and wrev differ in this particular case? Is one more computationally expensive than the other?
wrev(vect) -> vect(end:-1:1)
please try:
>> open wrev

请先登录,再进行评论。

更多回答(5 个)

This might work as well (For 1D Vectors):
vReversed = v(end:-1:1);
Good luck!

3 个评论

v(end:-1:1) subtracts each element from v(end) by 1.
Matt Eicholtz points out that Shweta's comment is incorrect; no subtraction is done, only indexing.
This is what I am looking for. Thank you.

请先登录,再进行评论。

Walter Roberson
Walter Roberson 2011-6-13

2 个投票

fliplr() or flipud()
... But I suspect this is a class assignment. You will need to use your knowledge of MATLAB indexing and looping to work out your assignments for yourself.
v = [1 2 3 4 5];
reversed = flip(v);
disp(reversed);
5 4 3 2 1
v = [10, 20, 30, 40, 50];
n = length(v);
for i = 1:n
rev(i) = v(n - i + 1);
end
disp('Reversed vector:');
Reversed vector:
disp(rev);
50 40 30 20 10

1 个评论

Doing this in a loop is very inefficient: avoid this approach!

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by