채택된 답변

Youssef  Khmou
Youssef Khmou 2013년 5월 29일

2 개 추천

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

Matthew Eicholtz
Matthew Eicholtz 2013년 6월 27일
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개)

Royi Avital
Royi Avital 2011년 6월 13일

6 개 추천

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

댓글 수: 3

Shweta Kanitkar
Shweta Kanitkar 2013년 5월 29일
v(end:-1:1) subtracts each element from v(end) by 1.
Walter Roberson
Walter Roberson 2013년 6월 28일
Matt Eicholtz points out that Shweta's comment is incorrect; no subtraction is done, only indexing.
Cyrus David Pastelero
Cyrus David Pastelero 2020년 7월 8일
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.
goga
goga 2011년 11월 19일

1 개 추천

use fliplr()
Varshitha Gouri
Varshitha Gouri 2026년 7월 7일

0 개 추천

v = [1 2 3 4 5];
reversed = flip(v);
disp(reversed);
5 4 3 2 1
DEVARAKONDA STEPHEN
DEVARAKONDA STEPHEN 2026년 7월 28일 9:45

0 개 추천

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

댓글 수: 2

Stephen23
Stephen23 2026년 7월 28일 10:24
Doing this in a loop is very inefficient: avoid this approach!
It's also incorrect in some cases, since rev was not preallocated.
v = (1:5).'
v = 5×1
1 2 3 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
n = length(v);
for i = 1:n
rev(i) = v(n - i + 1);
end
rev
rev = 1×5
5 4 3 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
whos v rev
Name Size Bytes Class Attributes rev 1x5 40 double v 5x1 40 double
In this case rev is the wrong size/shape! Compare with one of the recommended approach:
flippedV = flip(v)
flippedV = 5×1
5 4 3 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
whos flippedV v
Name Size Bytes Class Attributes flippedV 5x1 40 double v 5x1 40 double
In fact, the lack of preallocation may make the code error. Let me show the recommended approach first then show the error case. Because the previous section of code effectively "preallocated" rev, I need to clear it to simulate running the code in isolation.
v = zeros(1, 0);
flippedV = flip(v)
flippedV = 1×0 empty double row vector
clear rev % Simulate running the code without preallocation
n = length(v);
for i = 1:n
rev(i) = v(n - i + 1);
end
rev % doesn't exist, so this will error
Unrecognized function or variable 'rev'.

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

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2011년 6월 13일

댓글:

2026년 7월 28일 14:10

Community Treasure Hunt

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

Start Hunting!

Translated by