필터 지우기
필터 지우기

Swapping Blocks in a Matrix

조회 수: 4 (최근 30일)
Jordan Means
Jordan Means 2018년 9월 12일
댓글: Stephen23 2018년 9월 12일
Hello. I'm trying to swap the first two numbers of the first and second rows of this test matrix with the last two numbers of the second and third rows. Is there any way to swap these numbers in a matrix that's not one row and/or not on the same row?
x = [1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15]
The result should be:
x = [9 10 3 4 5;
14 15 8 1 2;
11 12 13 6 7]

채택된 답변

Stephen23
Stephen23 2018년 9월 12일
편집: Stephen23 2018년 9월 12일
>> x = [1,2,3,4,5;6,7,8,9,10;11,12,13,14,15]
x =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
>> x([1,4,11,14]) = x([11,14,1,4])
x =
9 10 3 4 5
6 7 8 1 2
11 12 13 14 15
You can easily generate those linear indices with sub2ind.
  댓글 수: 1
Jordan Means
Jordan Means 2018년 9월 12일
The code to do it was actually this:
x([1,2,4,5,11,12,14,15]) = x([11,12,14,15,1,2,4,5])
...but thanks! I got the gist of it!

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

추가 답변 (1개)

Bruno Luong
Bruno Luong 2018년 9월 12일
편집: Bruno Luong 2018년 9월 12일
[x(1:2,1:2) x(2:3,4:5)] = deal(x(2:3,4:5),x(1:2,1:2))
  댓글 수: 1
Stephen23
Stephen23 2018년 9월 12일
+1 neat use of deal.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by