gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:14 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com