What is the easiest way to remove a vector from a matrix?

5 次查看(过去 30 天)
Suppose there is a Loop A which returns a row vector y of the same number of columns such as matrix Q.
I want to remove all the occurrences of the row vectors y from Q, is their a simple way to do it (I am trying to avoid loops).
Here is a loop I wrote:
for i=1:size(Q,1)
s=eq(Q(i,:),y); r=all(s);
if r==1
Q(i,:)=[];
end
end

回答(2 个)

Chunru
Chunru 2021-9-11
编辑:Chunru 2021-9-11
a = magic(4);
a = [a; a(1, :); a]; % 1st 5th and 6th rows are the same
a
a = 9×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 16 2 3 13 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
y = a(1, :); % the vector input
idx = all(a == y, 2);
a(idx, :)=[]
a = 6×4
5 11 10 8 9 7 6 12 4 14 15 1 5 11 10 8 9 7 6 12 4 14 15 1
% in a line
% a(all(a==y, 2), :) =[]
  4 个评论
TADA
TADA 2021-9-14
this removes the lines that correspond to the indices in idx

请先登录,再进行评论。


TADA
TADA 2021-9-11
A = [magic(5); magic(5)];
v = A(1,:);
mask = A==v;
eqRows = all(mask, 2);
A(eqRows, :) = [];

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by