Remove a column from a matrix in GNU Octave

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.


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.

Tags:

Matrix

Octave