How to exclude rows on the basis of specific entries?

I have 260 rows and 16 columns with entries belong to {1,2,3,4}, I want to exclude those rows in which first four entries of each row is 4 , or in general how can I exclude rows on the basis of entries?

 Risposta accettata

It's not clear what you mean by repeat.
If you mean you want to delete rows whose first four columns contain more than one 4:
A = [4 4 1 2; 4 4 4 4;1 4 2 4;4 2 3 2]
todelete = sum(A(:, 1:4) == 4, 2) > 1;
A(todelete, :) = []
If you mean you want to delete rows whose first four columns contain two more more consecutive 4, then it's a lot more complicated. One possible way
A = [4 4 1 2; 4 4 4 4;1 4 2 4;4 2 3 2]
todelete = cellfun(@(row) ~isempty(strfind(row, [1 1])), num2cell(A(:, 1:4) == 4, 2));
A(todelete, :) = []

9 Commenti

Thank you very much. A = [4 4 1 2; 4 4 4 4;1 4 2 4;4 2 3 2]
todelete = sum(A(:, 1:4) == 4, 2) > 1;
A(todelete, :) = []
this works, But if I have 1000 rows and 16 columns and I want to delete all rows if any of the following holds:
1-There is more than one 4 in first four columns i-e (column 1 to column 4);
2-There is more than one 4 in column 5 to column 8;
3-There is more than one 4 column 9 to column 12;
4-There is more than one 4 in column 13 to column 16;
would you please help me.
assert(mod(size(A, 2), 4) == 0, 'Number of columns not a multiple of 4')
todelete = any(sum(reshape(A.', 4, [], size(A, 1)) == 4) > 1, 2);
A(todelete, :) = []
What I do here is:
  • A.': transpose A as it is easier to work on column.
  • reshape(...): then reshape into rows of 4 elements, each column corresponding to a group of four original columns and the third dimension the original rows of A
  • sum(... == 4): sum the number of 4s in each block
  • any(... > 1, 2): identify which original row has any block with more than one 4.
Thank you very much. GOD bless you always.
How can I exclude those rows in which all the entries are zero?
Sorry it does not work in my case , By taking simple example if I have a matrix like
0 0 1
0 0 0
0 0 2
0 1 1
0 2 0
Now in row 2 I have all the zero entries , so I want to exclude this row so that the resulting matrix becomes
0 0 1
0 0 2
0 1 1
0 2 0
Its a simple example for explanation I have thousands of rows and I want to delete all those rows having all the entries 0.
"it does not work"
!! In what way does it not work?
A = [0 0 1
0 0 0
0 0 2
0 1 1
0 2 0]
todelete = all(A == 0, 2);
A(todelete, :) = []
Sorry , its work. Thank you for the help.
A =
1 2 3 4 0 1 2 3
0 1 2 3 1 2 3 4
1 2 3 4 1 1 1 1
1 2 4 5 3 2 1 2
How can I separate those rows having first four entries are in the order 1 2 3 4 e.g in the above example I want to separate first and third row .

Accedi per commentare.

Più risposte (1)

Here is a flexible example:
% data
X = [4 4] ; % rows starting with this should be discarded
A = [1 4 1 4 ; 4 4 2 2 ; 4 2 4 0 ; 4 4 4 4 ; 2 4 4 2] ; % rows 2 and 4 should be discarded
% engine
tf = ismember(A(:,1:numel(X)), X, 'rows')
A(tf) = [] ; % remove

Richiesto:

il 6 Feb 2018

Commentato:

il 9 Feb 2018

Community Treasure Hunt

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

Start Hunting!

Translated by