Wednesday, June 20, 2007

Matlab: Diminishing the Size of a Matrix

Diminishing the Size of a Matrix


You can delete rows and columns from a matrix by assigning the empty array [] to those rows or columns. Start with
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1



Then, delete the second column of A using
A(:, 2) = []



This changes matrix A to
A =
16 3 13
5 10 8
9 6 12
4 15 1



If you delete a single element from a matrix, the result isn't a matrix anymore. So expressions like
A(1,2) = []



result in an error. However, you can use linear indexing to delete a single element, or a sequence of elements. This reshapes the remaining elements into a row vector:
A(2:2:10) = []



results in
A =
16 9 3 6 13 12 1

No comments: