Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何删除矩阵中的特定行_Matlab_Matrix_Matrix Indexing - Fatal编程技术网

Matlab 如何删除矩阵中的特定行

Matlab 如何删除矩阵中的特定行,matlab,matrix,matrix-indexing,Matlab,Matrix,Matrix Indexing,我有一个矩阵a,我想删除具有类似值的行(1,1),(2,2),(3,3) 所以矩阵是这样的 2 1 3 1 1 2 1 3 使用diff- A(diff(A,[],2)~=0,:) 对于一般的NXM情况,其中M是a的列数,可以将其扩展为- A(any(diff(A,[],2)~=0,2),:) 因此,如果你有 A= [1 1 1; 2 2 3; 3 1 4; 8 1 2; 2 2 2; 1 3 1;

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

所以矩阵是这样的

 2     1
 3     1
 1     2
 1     3

使用
diff
-

A(diff(A,[],2)~=0,:)
对于一般的
NXM
情况,其中
M
a
的列数,可以将其扩展为-

A(any(diff(A,[],2)~=0,2),:)
因此,如果你有

A= [1 1 1; 
    2 2 3; 
    3 1 4;
    8 1 2; 
    2 2 2; 
    1 3 1; 
    3 3 3]
你会得到-

 2     2     3
 3     1     4
 8     1     2
 1     3     1

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

 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.

或者
A=A(A(:,1)~=A(:,2),:)
,因为
=[]
方法可能效率低下:为了兴趣和未来的访问者,你应该在时间比较中包括你的原创作品
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.