Remove a column from a matrix in GNU Octave

MatrixOctave

Matrix Problem Overview


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!

Matrix Solutions


Solution 1 - Matrix

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.

Solution 2 - Matrix

GNU Octave delete Columns 2 and 4 from a Matrix

mymatrix = eye(5); 
mymatrix(:,[2,4]) = []; 
disp(mymatrix)

Prints:

1   0   0
0   0   0
0   1   0
0   0   0
0   0   1

                                                                      

GNU Octave delete Rows 2 and 4 from a Matrix:

mymatrix = eye(5); 
mymatrix([2,4],:) = [];
disp(mymatrix) 

Prints:

1   0   0   0   0
0   0   1   0   0
0   0   0   0   1

Time complexity

GNU Octave's CPU complexity for slicing and broadcasting here is a fast linear time O(n * c) where n is number of rows and c a constant number of rows that remain. It's C level single-core vectorized but not parallel.

Memory complexity

Working memory complexity is linear: O(n * 2) C makes a clone of the two objects, iterates over every element, then deletes the original.

The only time speed will be a problem is if your matrices are unrealistically wide, tall, or have a number of dimensions that blow out your fast memory, and speed is limited by the transfer speed between disk and memory.

Solution 3 - Matrix

The reverse method of doing this:

columns_you_want_to_keep = [1, 3, 5]
new_matrix = my_matrix(:,columns_you_want_to_keep)

Solution 4 - Matrix

How to remove multiple columns in octave:

How to remove columns 2 and 4:

columns_to_remove = [2 4];
matrix(:,columns_to_remove)=[]

Illustrated:

mymatrix = eye(5)
mymatrix =

   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



columns_to_remove = [2 4];

mymatrix(:,columns_to_remove)=[]


mymatrix =

   1   0   0
   0   0   0
   0   1   0
   0   0   0
   0   0   1 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric LeschinskiView Question on Stackoverflow
Solution 1 - MatrixporsView Answer on Stackoverflow
Solution 2 - MatrixHerrKaputtView Answer on Stackoverflow
Solution 3 - MatrixthethakuriView Answer on Stackoverflow
Solution 4 - MatrixEric LeschinskiView Answer on Stackoverflow