filter matrix according to column
2 次查看(过去 30 天)
显示 更早的评论
i have a matrix 10x60. It consists of entries with the number 0 or 1. I want to filter out all the rows, where the last five columns (55:60) have only 1 and no zero.
0 个评论
采纳的回答
Star Strider
2017-10-2
Try this:
M = randi([0 1], 10, 60); % Create Matrix
M(5, 55:60) = ones(1,6); % Row 5 Meets Criteria
row = all(M(:,55:60) == 1, 2); % Logical Vector, ‘1’ = Meets Criterion For Removal
Result = M(~row,:);
5 个评论
Jan
2017-10-4
@AA: Please include all details in the question already and do not provide more an more specifications later on. You did not ask for displaying anything before.
How do you want to display what?
Star Strider
2017-10-4
@Jan — Thank you! My sentiments exactly!
@AA — These added assignments produce a logical cell array of the rows deleted, and using the find function, the row numbers of the deleted rows:
M1 = randi([0 1], 10, 60); % Create Matrix
M2 = randi([0 1], 10, 60); % Create Matrix
M1(3, 55:60) = ones(1,6); % ‘M1’ Row 3 Meets Criteria
M2([5 7], 55:60) = ones(2,6); % ‘M2’ Rows 5,7 Meet Criteria
C = {M1, M2}; % Create Cell Array
Result = cellfun(@(M) M(~all(M(:,55:60) == 1, 2),:), C, 'Uni',0);
RowsToDeleteLogical = cellfun(@(M) (all(M(:,55:60) == 1,2)), C, 'Uni',0);
RowsToDeleteNumeric = cellfun(@find, RowsToDeleteLogical, 'Uni',0);
Keep them as cell arrays, since there may be different numbers of rows deleted in each matrix. Address the individual cells in ‘RowsToDeleteNumeric’ to find the row numbers of the deleted cells in the corresponding matrices.
(Away for a few hours doing other things, since I have a life outside MATLAB Answers. It took about 10 minutes to write and test the additional assignment functions.)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!