How can I delete certain rows of a matrix based on specific column values?

조회 수: 99 (최근 30일)
I have a matrix that has 6 columns and thousands of rows.
I need to delete the rows based on the following conditions:
1. if column 1 is zero then delete row
2. if column 2,3,4,and 5 is zero, and column 6 is not zero, then delete row
3. if column 2,3,and 4 is zero, and column 5 is not zero, then delete row
4. if column 2,and 3 is zero, and column 4 is not zero, then delete row
5. if column 2 is zero, and column 3 is not zero, then delete row
Someone please help, I feel like I've tried everything!

채택된 답변

Jos (10584)
Jos (10584) 2013년 11월 12일
This is a job for logical indexing! Note that you do not need to loop over all the lines at all
Assume A is your matrix. Here is a short example (not tested):
A = [0 2 3 4 5 6 ; 11 0 0 0 15 16 ; 21 0 0 0 0 26 ; 31 0 33 34 35 36 ; 41 42 43 0 0 0]
% Specify you conditions
TF1 = A(:,1)==0
TF2 = all(A(:,2:5)==0,2) & A(:,6) ~= 0
TF6 = A(:,2) == 0 & A(:,3) A ~= 0
% combine them
TFall = TF1 & TF2 & TF6
% remove
A(TFall,:) = []
  댓글 수: 8
Nathaniel H Werner
Nathaniel H Werner 2018년 5월 24일
Similar question.
What if you have a N by 3 array "A" and you need to remove M rows, where the length of M can vary? Can I make an M by 1 array of logicals (M by 1 because only need to worry about the row index at this point) and remove them from "A" in a similar fashion as was done above?

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

lis coffey
lis coffey 2016년 6월 24일
편집: Staff 2020년 8월 19일
To "delete"
matrix = eye(5);%5x5 identity matrix
list_o_cols_to_delete = [1 3 5];
matrix(:,list_o_cols_to_delete) = []
To only use the bits you want:
matrix = eye(5);%5x5 identity matrix
list_o_cols_to_use = [1 3 5];
used = matrix(:,list_o_cols_to_use)
-----------------------------------------------------------
Thanks

Andrei Bobrov
Andrei Bobrov 2013년 11월 12일
편집: Andrei Bobrov 2013년 11월 13일
Let S - your array.
S1 = S(S(:,1) ~= 0,:);
[ii,jj] = find( S1(:,2:end));
ss = sortrows([ii,jj],1);
idx = accumarray(ss(:,1),ss(:,2),[],@(x)x(1));
out = S1(idx < 2,:);
OR
out = S( all(S(:,1:2) ~= 0,2),:)

Walter Roberson
Walter Roberson 2013년 11월 12일
rownum = 5; %for example
if columns(rownum, 1) == 0;
columns(rownum,:) = [];
end
  댓글 수: 1
Teja Swaroop Naik Mudiki
Teja Swaroop Naik Mudiki 2016년 4월 12일
편집: per isakson 2016년 4월 12일
A = [0 2 3 4 5 6 ; 11 0 0 0 15 16 ; 21 0 0 0 0 26 ; 31 0 33 34 35 36 ; 41 42 43 0 0 0]
% Specify you conditions
TF1 = A(:,1)==0
TF2 = all(A(:,2:5)==0,2) & A(:,6) ~= 0
TF6 = A(:,2) == 0 & A(:,3) ~= 0
% combine them
TFall = TF1 | TF2 | TF6
% remove
A(TFall,:) = [] %#ok<*NOPTS>

댓글을 달려면 로그인하십시오.


Simon
Simon 2013년 11월 12일
Hi!
Suppose you have matrix M:
% logical vector for rows to delete
deleterow = false(size(M, 1), 1);
% loop over all lines
for n = 1:size(M, 1)
if M(n, 1) == 0
% mark line for deletion afterwards
deleterow(n) = true;
elseif (M(n, [2 3 4]) == [0 0 0]) & (M(n, 5) ~= 0)
deleterow(n) = true;
elseif % write every condition in a separate elseif
deleterow(n) = true;
end
end
% delete rows
M(deleterows, :) = [];
Be aware that the comparisn to 0 is valid for integers! If you have doubles you should instead write something like
tolerance = 1e-8;
if abs(M(n, 1) < tolerance
deleterow(n) = true;
end

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by