Switch two row of matrix
544 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dusan Grdic
le 5 Oct 2013
Commenté : Andrey Piavkin
le 27 Juin 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 commentaires
madhan ravi
le 21 Fév 2019
Modifié(e) : madhan ravi
le 21 Fév 2019
A more efficient answer had been accepted 5 years ago.
Réponse acceptée
Azzi Abdelmalek
le 5 Oct 2013
Modifié(e) : Azzi Abdelmalek
le 5 Oct 2013
A=[1 3 4; 2 4 5]
A([1 2],:)=A([2 1],:)
3 commentaires
Andrey Piavkin
le 27 Juin 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
Plus de réponses (1)
Pontus Vikstål
le 12 Août 2019
Modifié(e) : Pontus Vikstål
le 12 Août 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 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!