Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
reversing a vector

Subject: reversing a vector

From: Andre' Pampel

Date: 29 Jun, 1998 19:14:24

Message: 1 of 8

Hi there,
does anybody have an idea how to "reverse" a vector :
a=[1 2 3 4 ... i-1 i] -rev.-> b=[i i-1 ... 4 3 2 1]
without using for-loops?
Thanks
Andre'
--
-----------------------------------------------------------------
Andre' Pampel
University of Leipzig
Faculty of Physics and Geosciences
Linnestrasse 5
04103 Leipzig
Tel/Fax.: ++49-341-9732478/ ++49-341-9732479
email: anpa@rz.uni-leipzig.de
        http://www.uni-leipzig.de
-----------------------------------------------------------------

Subject: reversing a vector

From: Simon A Watts

Date: 29 Jun, 1998 09:54:47

Message: 2 of 8

Andre' Pampel wrote in message <3597CB70.41C6@rz.uni-leipzig.de>...
>Hi there,
>does anybody have an idea how to "reverse" a vector :
>a=[1 2 3 4 ... i-1 i] -rev.-> b=[i i-1 ... 4 3 2 1]
>without using for-loops?


b = a(end:-1:1); % vector, MATLAB 5 only
b = a(end:-1:1,end:-1:1); % 2D array, MATLAB 5 only

b = flipud(a); % flip up-down, 'a' is a column vector
b = fliplr(a); % flip left-right, 'a' is a row vector
b = flipud(fliplr(a)); % 'a' is a 2D matrix

Si.

/********************************************************
 * Simon A Watts *
 * Defence Evaluation and Research Agency *
 * *
 * email: sawatts@dera.gov.uk DERA Farnborough *
 * Tel: +44 (0) 1252 392876 Ively Road *
 * Fax: +44 (0) 1252 395090 Farnborough *
 * HANTS GU14 0LX *
 * United Kingdom *
 * *
 * What makes you think DERA and I share any opinions? *
 ********************************************************/

Subject: reversing a vector

From: Fabio Luna

Date: 29 Jun, 1998 12:00:28

Message: 3 of 8



Andre' Pampel wrote:

> Hi there,
> does anybody have an idea how to "reverse" a vector :
> a=[1 2 3 4 ... i-1 i] -rev.-> b=[i i-1 ... 4 3 2 1]
> without using for-loops?
>

Hi Andre,

Try the function 'fliplr' which returns columns flipped in the
left-right direction. If you need to flip matrices upside-down try
'flipud'.

--

Fabio Luna

__________________________________________________________________________

Dept. Chemical Engineering and Technology | Phone: +468 790 6569
Division of Transport Phenomena | Fax: +468 21 27 47
Royal Institute of Technology (KTH) | E-mail: fabio@ket.kth.se
Teknikringen 36 |
100 44 Stockholm |
Sweden |
__________________________________________________________________________

Subject: reversing a vector

From: Neil Judell

Date: 29 Jun, 1998 06:28:05

Message: 4 of 8

very simple -- here's a cut and paste from an example typed into the Matlab
command window (version 5.2.1)

» a=1:12;
» b=a(end:-1:1)

b =

    12 11 10 9 8 7 6 5 4 3 2 1

»
--neil
"Where are we going, and why am I in this handbasket?"
Andre' Pampel wrote in message <3597CB70.41C6@rz.uni-leipzig.de>...
>Hi there,
>does anybody have an idea how to "reverse" a vector :
>a=[1 2 3 4 ... i-1 i] -rev.-> b=[i i-1 ... 4 3 2 1]
>without using for-loops?
>Thanks

Subject: reversing a vector

From: samtoon@my-dejanews.com

Date: 29 Jun, 1998 10:52:51

Message: 5 of 8

In article <3597CB70.41C6@rz.uni-leipzig.de>,
  Andre' Pampel <anpa@rz.uni-leipzig.de> wrote:
>
> Hi there,
> does anybody have an idea how to "reverse" a vector :
> a=[1 2 3 4 ... i-1 i] -rev.-> b=[i i-1 ... 4 3 2 1]
> without using for-loops?

If it's a row vector b = fliplr(a)

If it's a column vector b = flipud(a)

Sam

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Subject: reversing a vector

From: uchiyama@ics.kagoshima-u.ac.jp (Hiroyuki UCHIYAMA)

Date: 29 Jun, 1998 18:58:00

Message: 6 of 8

In article <3597CB70.41C6@rz.uni-leipzig.de>, Andre' Pampel <anpa@rz.uni-leipzig.de> wrote:

> does anybody have an idea how to "reverse" a vector :
> a=[1 2 3 4 ... i-1 i] -rev.-> b=[i i-1 ... 4 3 2 1]
> without using for-loops?

How about b=a(i:-1:1)?

>>a=1:5
a =
 1 2 3 4 5
>>b=a(5:-1:1)
b =
 5 4 3 2 1

I hope this helps.

--
Hiroyuki Uchiyama, PhD
ICS, Kagoshima Univ., JAPAN
http://www.ics.kagoshima-u.ac.jp/~uchiyama

Subject: reversing a vector

From: anderson@python.ucsd.edu (Greg Anderson)

Date: 29 Jun, 1998 16:11:50

Message: 7 of 8

In article <6n7kjt$gmv$1@trog.dra.hmg.gb>,
Simon A Watts <sawatts@dera.gov.REMOVEME.uk> wrote:
>
>b = a(end:-1:1); % vector, MATLAB 5 only
>b = a(end:-1:1,end:-1:1); % 2D array, MATLAB 5 only

Actually, in my MATLAB 4.2c under HPUX 9.07, this works just fine,
as do the following lines. But, for some reason, I've never
tried this before --- thanks, Simon!

>b = flipud(a); % flip up-down, 'a' is a column vector
>b = fliplr(a); % flip left-right, 'a' is a row vector
>b = flipud(fliplr(a)); % 'a' is a 2D matrix


Greg Anderson
anderson@python.ucsd.edu

Subject: reversing a vector

From: rgorman@jjmsystems.com (Rich)

Date: 29 Jun, 1998 16:20:28

Message: 8 of 8

For vectors you can use

 b = rot90(a,2)

so you don't have to worry about whether it's a column or row vector.

Rich

"Simon A Watts" <sawatts@dera.gov.REMOVEME.uk> wrote:

>Andre' Pampel wrote in message <3597CB70.41C6@rz.uni-leipzig.de>...
>>Hi there,
>>does anybody have an idea how to "reverse" a vector :
>>a=[1 2 3 4 ... i-1 i] -rev.-> b=[i i-1 ... 4 3 2 1]
>>without using for-loops?


>b = a(end:-1:1); % vector, MATLAB 5 only
>b = a(end:-1:1,end:-1:1); % 2D array, MATLAB 5 only

>b = flipud(a); % flip up-down, 'a' is a column vector
>b = fliplr(a); % flip left-right, 'a' is a row vector
>b = flipud(fliplr(a)); % 'a' is a 2D matrix

>Si.

>/********************************************************
> * Simon A Watts *
> * Defence Evaluation and Research Agency *
> * *
> * email: sawatts@dera.gov.uk DERA Farnborough *
> * Tel: +44 (0) 1252 392876 Ively Road *
> * Fax: +44 (0) 1252 395090 Farnborough *
> * HANTS GU14 0LX *
> * United Kingdom *
> * *
> * What makes you think DERA and I share any opinions? *
> ********************************************************/

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us