Borrar filtros
Borrar filtros

Flipping Values (1 Line)

1 visualización (últimos 30 días)
Rooy
Rooy el 12 de Oct. de 2013
Comentada: Rooy el 12 de Oct. de 2013
I have to create this program which flips two values and both are inputted by the user. I have to flip the value in a single line of code in the for loop. I have figured it using three lines of code but one line in the for loop is confusing me.
user1=input('')
user2=input('')
for total=1:5
.........
end
Basic Output
user1=1
userr2=2
user1=2
userr2=1
user1=1
userr2=2
  2 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 12 de Oct. de 2013
Why in a for loop?
Rooy
Rooy el 12 de Oct. de 2013
It is required unless there is another way

Iniciar sesión para comentar.

Respuesta aceptada

Jos (10584)
Jos (10584) el 12 de Oct. de 2013
Like other, I totally fail to see why you need a for loop ... Anyways, this will work
for k=1,
[b,a] = deal(a,b)
end
  1 comentario
Rooy
Rooy el 12 de Oct. de 2013
Thank you very much

Iniciar sesión para comentar.

Más respuestas (2)

Jos (10584)
Jos (10584) el 12 de Oct. de 2013
Hint:
A(1) = 1
A(2) = 2
A([2 1])
  1 comentario
Rooy
Rooy el 12 de Oct. de 2013
I kind of need a single line of code in a for loop that does the trick. Anyway really appreciate your help. Thank you

Iniciar sesión para comentar.


sixwwwwww
sixwwwwww el 12 de Oct. de 2013
Dear Rooy, here is the desired code without for loop:
a = input('Input first value: ');
b = input('Input second value: ');
c = a;
a = b;
b = c;
disp(strcat('First value is : ', num2str(a)))
disp(strcat('Second value is: ', num2str(b)))
  1 comentario
Rooy
Rooy el 12 de Oct. de 2013
Thank you , that is exactly what I did but I have to do it in one line of code

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by