Effacer les filtres
Effacer les filtres

filter matrix according to column

1 vue (au cours des 30 derniers jours)
AA
AA le 2 Oct 2017
Commenté : Star Strider le 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.

Réponse acceptée

Star Strider
Star Strider le 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,:);
  5 commentaires
Jan
Jan le 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
Star Strider le 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.)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by