matlab - 如何删除矩阵中的特定行

标签 matlab matrix matrix-indexing

我有一个矩阵 A ,我想删除具有相似值的行 (1,1), (2,2), (3 ,3)

A =
     1     1
     2     1
     3     1
     1     2
     2     2
     1     3
     3     3

所以矩阵应该是这样的

 2     1
 3     1
 1     2
 1     3

最佳答案

另一种不调用任何函数的方法:

 A = A(A(:,1) == A(:,2),:)

此方法与基于diff() 的解决方案的效率:

n = 10;
y = [round(rand(n,1)) round(rand(n,1))];

tic;
for i = 1:1e4
  A = y;
  A(diff(A,[],2)~=0,:);
end
toc
Elapsed time is 0.091990 seconds.

tic;
for i = 1:1e4
  A = y;
  A = A(A(:,1) == A(:,2),:);
end
toc
Elapsed time is 0.037842 seconds.



% Suggestion of @Dan in the comments
tic;
for i = 1:1e4
  A = y;
  A(A(:,1) == A(:,2),:) = [];
end
toc
Elapsed time is 0.147636 seconds.

关于matlab - 如何删除矩阵中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24778509/

相关文章:

python - 用 Pandas 计算矩阵的逆

matlab - 为什么我收到错误 "Index exceeds matrix dimensions"?

python - Numpy 重新索引前 N 个自然数

matlab - 从 MATLAB 中的 GUI 更改图形的 x 轴和 y 轴颜色

c# - XNA/C# 世界矩阵中的 2D 坐标缩放到 3D View 矩阵?

image - 如何在 Matlab 中制作高斯滤波器

c# - 如何计算矩阵行列式? n*n 或者只是 5*5

matlab - 求矩阵元素匹配条件的索引 - Matlab

Matlab:在被另一个函数调用时从回调函数获取 GUI 句柄值

matlab - 如何使用 matlab 正确绘制矢量线性方程?