Wednesday, July 22, 2009

Deleting Rows and Columns

You can delete rows and columns from a matrix using just a pair of square brackets. Start with
X = A;
Then, to delete the second column of X, use
X(:,2) = []
This changes X to
X =
16 2 13
5 11 8
9 7 12
4 14 1

If you delete a single element from a matrix, the result is not a matrix anymore. So, expressions like X(1,2) = []result in an error. However, using a single subscript deletes a single element, or sequence of elements, and reshapes the remaining elements into a row vector.
So X(2:2:10) = []
results in X = 16 9 2 7 13 12 1

No comments:

Post a Comment