Programming  Reshaping a Matrix

The following functions change the shape of a matrix.

 Function Description `reshape` Modify the shape of a matrix. `rot90` Rotate the matrix by 90 degrees. `fliplr` Flip the matrix about a vertical axis. `flipud` Flip the matrix about a horizontal axis. `flipdim` Flip the matrix along the specified direction. `transpose` Flip a matrix about its main diagonal, turning row vectors into column vectors and vice versa. `ctranspose` Transpose a matrix and replace each element with its complex conjugate.

Examples

Here are a few examples to illustrate some of the ways you can reshape matrices.

Reshaping a Matrix.   Reshape 3-by-4 matrix `A` to have dimensions 2-by-6:

• ```A = [1 4 7 10; 2 5 8 11; 3 6 9 12]
A =
1    4    7    10
2    5    8    11
3    6    9    12

B = reshape(A, 2, 6)
B =
1    3    5    7    9   11
2    4    6    8   10   12
```

Transposing a Matrix.   Transpose `A` so that the row elements become columns. You can use either the `transpose` function or the transpose operator (.`'`) to do this:

• ```B = A.'
B =
1     2     3
4     5     6
7     8     9
10    11    12
```

There is a separate function called `ctranspose` that performs a complex conjugate transpose of a matrix. The equivalent operator for `ctranpose` on a matrix `A` is `A'`:

• ```A = [1+9i 2-8i 3+7i; 4-6i 5+5i 6-4i]
A =
1.0000 + 9.0000i   2.0000 -8.0000i   3.0000 + 7.0000i
4.0000 -6.0000i   5.0000 + 5.0000i   6.0000 -4.0000i

B = A'
B =
1.0000 -9.0000i   4.0000 + 6.0000i
2.0000 + 8.0000i   5.0000 -5.0000i
3.0000 -7.0000i   6.0000 + 4.0000i
```

Rotating a Matrix.   Rotate the matrix by 90 degrees:

• ```B = rot90(A)
B =
10    11    12
7     8     9
4     5     6
1     2     3
```

Flipping a Matrix.   Flip `A` in a left-to-right direction:

• ```B = fliplr(A)
B =
10     7     4     1
11     8     5     2
12     9     6     3
``` Diminishing the Size of a Matrix Preallocating Memory © 1994-2005 The MathWorks, Inc.