Filter löschen
Filter löschen

filter matrix according to column

1 Ansicht (letzte 30 Tage)
AA
AA am 2 Okt. 2017
Kommentiert: Star Strider am 4 Okt. 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.

Akzeptierte Antwort

Star Strider
Star Strider am 2 Okt. 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 Kommentare
Jan
Jan am 4 Okt. 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 am 4 Okt. 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.)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by