filter matrix according to column

1 visualización (últimos 30 días)
AA
AA el 2 de Oct. de 2017
Comentada: Star Strider el 4 de Oct. de 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.

Respuesta aceptada

Star Strider
Star Strider el 2 de Oct. de 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 comentarios
Jan
Jan el 4 de Oct. de 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 el 4 de Oct. de 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.)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by