Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a column from a matrix in GNU Octave

Tags:

matrix

octave

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!

like image 934
Eric Leschinski Avatar asked Sep 12 '12 14:09

Eric Leschinski


People also ask

How do you remove a column from a matrix?

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.


1 Answers

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.

like image 69
pors Avatar answered Sep 20 '22 22:09

pors