Switch two row of matrix

483 visualizaciones (últimos 30 días)
Dusan Grdic
Dusan Grdic el 5 de Oct. de 2013
Comentada: Andrey Piavkin el 27 de Jun. de 2021
How to switch two row of matrix? For example: matrix A is: 1 3 4; 2 4 5 and Y want to be 2 4 5; 1 3 4 ?
  3 comentarios
madhan ravi
madhan ravi el 21 de Feb. de 2019
Editada: madhan ravi el 21 de Feb. de 2019
A more efficient answer had been accepted 5 years ago.
Tristan McRae
Tristan McRae el 22 de Feb. de 2019
omg savage

Iniciar sesión para comentar.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 5 de Oct. de 2013
Editada: Azzi Abdelmalek el 5 de Oct. de 2013
A=[1 3 4; 2 4 5]
A([1 2],:)=A([2 1],:)
  3 comentarios
Kevin Moss
Kevin Moss el 12 de Jun. de 2021
can anyone explain the logic behind why/how this works?
Andrey Piavkin
Andrey Piavkin el 27 de Jun. de 2021
this works pretty clear
we take first and second all the row and make it equal to second and first all the row
for example, if you wanted to switch the third and the first row, you should type like this:
A([1 3], :) = A([3 1], :)
where ':' at second place stands for taking all the columns and '[1 3]' stands for first and third row

Iniciar sesión para comentar.

Más respuestas (1)

Pontus Vikstål
Pontus Vikstål el 12 de Ag. de 2019
Editada: Pontus Vikstål el 12 de Ag. de 2019
This way might be even faster.
A = [1 3 4; 2 4 5]
x = [0 1;1 0];
A = x*A
Then there's also this way
A = [1 3 4; 2 4 5]
A = flip(A)
  2 comentarios
madhan ravi
madhan ravi el 25 de Jul. de 2020
If A has more than two rows this won’t work.
Luis Mendez Lopez
Luis Mendez Lopez el 22 de Sept. de 2020
Yeah i will do that with my 600*600 matrix

Iniciar sesión para comentar.

Categorías

Más información sobre Logical 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