我有一个矩阵 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/