# filter matrix according to column

25 views (last 30 days)
AA on 2 Oct 2017
Commented: Star Strider on 4 Oct 2017
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.

Star Strider on 2 Oct 2017
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,:);

AA on 4 Oct 2017
thanks but the row number within the matrix is not displayed in your latest example.
Jan on 4 Oct 2017
@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 on 4 Oct 2017
@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.)