在GNU Octave中从矩阵中删除列

问题描述:

在GNU 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.

假设我有这个:

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

我想删除第2列和第4列,但是当我删除第2列时,第4列的位置已移至第3列,这使我的头部受伤.必须有更好的方法!

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!

GNU Octave从矩阵中删除第2列和第4列

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

打印:

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

GNU Octave从矩阵中删除第2行和第4行:

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

打印:

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

时间复杂度

此处进行切片和广播的GNU Octave的CPU复杂度是快速线性时间O(n * c),其中n是行数,c是剩余的行数.它是C级单核矢量化的,但不是并行的.

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.

内存复杂度

工作内存的复杂度是线性的:O(n * 2) C对两个对象进行克隆,遍历每个元素,然后删除原始对象.

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.