MATLAB: Swapping Blocks in a Matrix

columnMATLABmatrixrowswap

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]

Best Answer

>> 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.