Dimitris
Dimitris

Reputation: 579

Swap elements in Matlab as in Python

I am new in Matlab. In Python one can swap elements in a handy way.

x, y = 5, 10

x, y = y, x

Is there something similar in Matlab (or in Octave/Scilab)? Otherwise, what is the best way to swap elements without the use of a temp variable?

Upvotes: 4

Views: 182

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

deal is the function that you're looking for.

[y,x] = deal(x,y);

Example:

x=5; y=10;
[y,x] = deal(x,y)

y =
     5

x =    
    10

Upvotes: 5

Related Questions