Reverse vector in matlab

Hi all,
I have a vector z=0:dz:h, which I want to reverse so I get z*=h:dz:0.
For example if
h=5 and dz=1, then z=0 1 2 3 4 5
I want to obtain z=5 4 3 2 1
Does anyone know a smart way to achieve this in Matlab?
Thanks!

댓글 수: 2

James Tursa
James Tursa 2013년 2월 14일
Do you want the 0 on the end? I.e., is your 5 4 3 2 1 a typo and you really mean 5 4 3 2 1 0?
Ellen
Ellen 2013년 2월 18일
Sorry, it is a typo indeed! Thanks!

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

 채택된 답변

James Tursa
James Tursa 2013년 2월 14일

4 개 추천

z = fliplr(z);
or
z = h:-dz:0;

댓글 수: 5

Jan
Jan 2013년 2월 14일
편집: Jan 2013년 2월 14일
Inlining fliplr reduces the overhead of calling a function:
z = z(numel(z):-1:1);
but ... numel is also a function. Another option:
z = z(end:-1:1)
Just to be thorougly anal, end is a function as well. Just try
edit end
Jan
Jan 2013년 2월 18일
편집: Jan 2013년 2월 18일
end is significantly slower than a length command. In addition I've seen so many bugs related to end in Matlab's bug-reports, that I avoid it even in the trivial cases.
function mySillyTest
x = rand(1, 1000);
tic;
for i = 1:1e6
c = x(end);
end
toc
tic;
for i = 1:1e6
c = x(numel(x));
end
toc
% Matlab 2009a/64, Win7, Core2Duo
Elapsed time is 0.016512 seconds.
Elapsed time is 0.005833 seconds.
% R2011b:
Elapsed time is 0.014812 seconds.
Elapsed time is 0.005928 seconds.
But: "significantly" faster for this tiny detail does usually not mean, that the total time of a program will be effected significantly.
Jos (10584)
Jos (10584) 2013년 2월 18일
Just to extra-thoroughly anal, I never said that end is not a function ;-)
I concur with Jan's opinion about the use of end ...

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2013년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by