What is the easiest way to remove a vector from a matrix?
조회 수: 7 (최근 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
댓글 수: 0
답변 (2개)
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
y = a(1, :); % the vector input
idx = all(a == y, 2);
a(idx, :)=[]
% in a line
% a(all(a==y, 2), :) =[]
댓글 수: 4
TADA
2021년 9월 14일
this removes the lines that correspond to the indices in idx
I suggest you read the documentation on indexing
TADA
2021년 9월 11일
A = [magic(5); magic(5)];
v = A(1,:);
mask = A==v;
eqRows = all(mask, 2);
A(eqRows, :) = [];
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!