swapping rows in a matrix

조회 수: 35 (최근 30일)
Stijn Bier
Stijn Bier 2019년 11월 26일
댓글: Adam Danz 2020년 8월 11일
How can i create a matrix which is a copy of an other matrix except 2 rows have to swap.
for example 4*4nmatrix called M how can i create an new matrix which is a copy of M, but the first and the third row are swapped

채택된 답변

Stephen23
Stephen23 2019년 11월 26일
>> X = [1,3]; % rows to swap
>> M = randi(9,4,5)
M =
6 7 6 2 9
2 3 2 1 1
1 6 9 7 9
4 6 7 5 2
>> W = M;
>> W(X,:) = W(X([2,1]),:)
W =
1 6 9 7 9
2 3 2 1 1
6 7 6 2 9
4 6 7 5 2
  댓글 수: 3
Narayan Sharma
Narayan Sharma 2020년 8월 11일
what does the [2,1] call for
Adam Danz
Adam Danz 2020년 8월 11일
With the syntax W(X([A,B]),:) row A becomes row 1 and row B becomes row 2.
Indexing is lesson 1 in Matlab.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by