MATLAB Answers

Leah
2

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

Asked by Leah
on 12 Nov 2013
Latest activity Commented on by Nathaniel H Werner on 24 May 2018
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!

  0 Comments

Sign in to comment.

5 Answers

Answer by Jos (10584)
on 12 Nov 2013
 Accepted Answer

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,:) = []

  7 Comments

You can keep a copy of A
B = A ;
B(TfFall,:) = [] ;
and work with B in the remainder of the code.
Note that Jan's answer on this question gives a few different methods and the time efficiency of each (on Win7_64: your mileage may vary).
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.


Answer by lis coffey on 24 Jun 2016
Edited by Geoff Hayes
on 12 Sep 2016

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

  0 Comments

Sign in to comment.


Answer by Andrei Bobrov
on 12 Nov 2013
Edited by 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),:)

  1 Comment

Most usefully. Thanks.

Sign in to comment.


Answer by Walter Roberson
on 12 Nov 2013

rownum = 5; %for example
if columns(rownum, 1) == 0;
columns(rownum,:) = [];
end

  1 Comment

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.


Answer by 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

  0 Comments

Sign in to comment.