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

New to MATLAB?

how to interchange array elements

Asked by Sivakumaran Chandrasekaran

Sivakumaran Chandrasekaran

on 28 Mar 2013

my input is x=[11 12 13 14 15]. my output should be y=[15 12 13 14 11]. how to interchange particular values without disturbing other elements.?

0 Comments

Sivakumaran Chandrasekaran

Sivakumaran Chandrasekaran

Tags

Products

2 Answers

Answer by Andrei Bobrov

Andrei Bobrov

on 28 Mar 2013
x = [11 12 13 14 15];
y = x;
y([end,1]) = y([1,end]);

0 Comments

Andrei Bobrov

Andrei Bobrov

Answer by Honglei Chen

Honglei Chen

on 28 Mar 2013

Not sure if this is what you are looking for, but here is one way to do it, as long as you know the order

y = x([5 2 3 4 1])

2 Comments

no. first element and last element should be interchanged is my objective..

Cedric Wannaz

Cedric Wannaz

on 28 Mar 2013
 y = [x(end), x(2:end-1), x(1)] ;
Honglei Chen

Honglei Chen

Contact us