Swapping Blocks in a Matrix
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jordan Means
el 12 de Sept. de 2018
Comentada: Stephen23
el 12 de Sept. de 2018
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]
0 comentarios
Respuesta aceptada
Stephen23
el 12 de Sept. de 2018
Editada: Stephen23
el 12 de Sept. de 2018
>> 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 comentario
Más respuestas (1)
Bruno Luong
el 12 de Sept. de 2018
Editada: Bruno Luong
el 12 de Sept. de 2018
[x(1:2,1:2) x(2:3,4:5)] = deal(x(2:3,4:5),x(1:2,1:2))
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!