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

111 ビュー (過去 30 日間)
Leah
Leah 2013 年 11 月 12 日
コメント済み: Xhino MELEQI 2022 年 10 月 24 日
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?
Xhino MELEQI
Xhino MELEQI 2022 年 10 月 24 日
What a genius ! thanks

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by