### Tags: code, matlab, operation, programming, quicklyif, reverse, vector

# quick code to reverse a vector

On Programmer » Matlab

1,862 words with 5 Comments; publish: Wed, 07 May 2008 09:44:00 GMT; (20046.88, « »)

I want to do this operation quickly:

if a is [3 4 6]

I need the reverse: [6 4 3]

Thanks

*http://matlab.todaysummary.com/q_matlab_49631.html*

All Comments

Leave a comment...

- 5 Comments
- Hi,
M=[3 4 6]

M=M(end:-1:1)

Jrme

#1; Wed, 07 May 2008 09:45:00 GMT

- Hi,
- b83503104.matlab.todaysummary.com.yahoo.com:
<SNIP vec-flipper evergreen...

one of the many solutions

v=[4,5,10];

vr=v(end:-1:1)

us

#2; Wed, 07 May 2008 09:46:00 GMT

- b83503104.matlab.todaysummary.com.yahoo.com:
- b83503104.matlab.todaysummary.com.yahoo.com wrote:
> I want to do this operation quickly:

> if a is [3 4 6]

> I need the reverse: [6 4 3]

> Thanks

>

The standard way to do it in Matlab is fliplr.

I think you may get some speed using this function instead

function y = fliplr1(x)

y = x(end:-1:1);

This is the version of fliplr without input checking and it returns one

dimensional array.

This is how you can measure time required

x = rand(1, 10);

tic

for q=1:1e4

%x = fliplr(x)

x = fliplr1(x)

end

toc

#3; Wed, 07 May 2008 09:47:00 GMT

- b83503104.matlab.todaysummary.com.yahoo.com wrote:
- Sergey Litvinov wrote:
>

> The standard way to do it in Matlab is fliplr.

fliplr only works for row vectors. The function fliparray on the

MATLAB File Exchange works for any vector, row or column.

<http://www.mathworks.com/matlabcent...>

#4; Wed, 07 May 2008 09:48:00 GMT

- Sergey Litvinov wrote:
- Duane Hanselman wrote:
> Sergey Litvinov wrote:

>

>

>

> fliplr only works for row vectors. The function fliparray on the

> MATLAB File Exchange works for any vector, row or column.

> <http://www.mathworks.com/matlabcent...

Thank you.

Very smart code!

#5; Wed, 07 May 2008 09:49:00 GMT

- Duane Hanselman wrote: