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!
0 个评论
采纳的回答
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
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
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
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
2013-11-12
rownum = 5; %for example
if columns(rownum, 1) == 0;
columns(rownum,:) = [];
end
1 个评论
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
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!