In GNU Octave, I want to be able to remove specific columns from a matrix. In the interest of generality. I also want to be able to remove specific rows from a matrix.
Suppose I have this:
mymatrix = eye(5) mymatrix = Diagonal Matrix 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
I want to remove columns 2 and 4, but when I remove column 2, the position of column 4 has moved to column 3, and that makes my head hurt. There has to be a better way!
The easiest way to remove a row or column from a matrix is to set that row or column equal to a pair of empty square brackets [] . For example, create a 4-by-4 matrix and remove the second row. Now remove the third column.
In case you don't know the exact number of columns or rows you can use the magic "end" index, e.g.:
mymatrix(:,2:end) % all but first column mymatrix(2:end,:) % all but first row
This also allows you to slice rows or columns out of a matrix without having to reassign it to a new variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With