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

Nikhil Sachan
Nikhil Sachan 2019년 2월 1일
temp=A(2,:);
A(1,:)=A(2,:);
A(2,:)=temp;
%this will swap required rows
madhan ravi
madhan ravi 2019년 2월 21일
편집: madhan ravi 2019년 2월 21일
A more efficient answer had been accepted 5 years ago.
Tristan McRae
Tristan McRae 2019년 2월 22일
omg savage

댓글을 달려면 로그인하십시오.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 5일
편집: Azzi Abdelmalek 2013년 10월 5일

28 개 추천

A=[1 3 4; 2 4 5]
A([1 2],:)=A([2 1],:)

댓글 수: 3

Evil Meet
Evil Meet 2020년 10월 27일
Thank you very much. U r awesome
Kevin Moss
Kevin Moss 2021년 6월 12일
can anyone explain the logic behind why/how this works?
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일

1 개 추천

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2013년 10월 5일

댓글:

2021년 6월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by