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

99 views (last 30 days)
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!

Accepted Answer

Jos (10584)
Jos (10584) on 12 Nov 2013
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 Comments
Nathaniel H Werner
Nathaniel H Werner on 24 May 2018
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?

Sign in to comment.

More Answers (4)

lis coffey
lis coffey on 24 Jun 2016
Edited: Staff on 19 Aug 2020
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 on 12 Nov 2013
Edited: Andrei Bobrov on 13 Nov 2013
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 on 12 Nov 2013
rownum = 5; %for example
if columns(rownum, 1) == 0;
columns(rownum,:) = [];
end
  1 Comment
Teja Swaroop Naik Mudiki
Teja Swaroop Naik Mudiki on 12 Apr 2016
Edited: per isakson on 12 Apr 2016
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>

Sign in to comment.


Simon
Simon on 12 Nov 2013
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

Community Treasure Hunt

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

Start Hunting!