Yauhen Yakimovich
Yauhen Yakimovich

Reputation: 14211

how to reverse/inverse order of elements in a vector or a matrix in matlab?

I have a vector

a = 1:4

I want to get

[4 3 2 1]

Which matlab function should I use?

Upvotes: 7

Views: 19786

Answers (3)

Joce
Joce

Reputation: 2332

If the array you want to reverse is a range a:b, then it is much faster to use:

-(-b:-a)

Upvotes: 0

user2041376
user2041376

Reputation:

another option is to use indexing

a(end:-1:1)

Upvotes: 7

Yauhen Yakimovich
Yauhen Yakimovich

Reputation: 14211

For a row use

fliplr(a)

for column use

flipud(a)

Upvotes: 13

Related Questions