Switch two row of matrix

499 次查看(过去 30 天)
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 个评论
madhan ravi
madhan ravi 2019-2-21
编辑:madhan ravi 2019-2-21
A more efficient answer had been accepted 5 years ago.

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-10-5
编辑:Azzi Abdelmalek 2013-10-5
A=[1 3 4; 2 4 5]
A([1 2],:)=A([2 1],:)
  3 个评论
Kevin Moss
Kevin Moss 2021-6-12
can anyone explain the logic behind why/how this works?
Andrey Piavkin
Andrey Piavkin 2021-6-27
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

请先登录,再进行评论。

更多回答(1 个)

Pontus Vikstål
Pontus Vikstål 2019-8-12
编辑:Pontus Vikstål 2019-8-12
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 个评论
madhan ravi
madhan ravi 2020-7-25
If A has more than two rows this won’t work.
Luis Mendez Lopez
Luis Mendez Lopez 2020-9-22
Yeah i will do that with my 600*600 matrix

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by