0
votes

Assuming I have a 23x3 matrix which contains both integers and non-integers. I would ideally want to remove some of the matrix rows based on the following criteria:

  • Remove row if any 2 columns are non-integers
  • Remove row if all 3 columns are non-integers
  • Remove row if all columns are integers

The above implies that the rows that will be left should have only one non-integer and two integers

Below is my matrix:

    A = [1  1.5 1
         1  2.5 1
         1  3.5 1
         1  1   1.5
         1  1.5 1.5
         1  2   1.5
         1  2.5 1.5
         1  3   1.5
         1  3.5 1.5
         1  4   1.5
         1  1.5 2
         1  2.5 2
         1  3.5 2
         1  1   2.5
         1  1.5 2.5
         1  2   2.5
         1  2.5 2.5
         1  3   2.5
         1  3.5 2.5
         1  4   2.5
         1  1.5 3
         1  2.5 3
         1  3.5 3];

My final output should be:

B = [1  1.5 1  
     1  2.5 1
     1  3.5 1
     1  1   1.5
     1  2   1.5
     1  3   1.5
     1  4   1.5
     1  1.5 2
     1  2.5 2
     1  3.5 2
     1  1   2.5
     1  2   2.5
     1  3   2.5
     1  4   2.5
     1  1.5 3
     1  2.5 3
     1  3.5 3];

I am still learning and still trying to find my feet on which way to go. Please, guys help me out with this one. Thank you!

1
Do you know how to remove columns according to one criteria? Just do the same, but use the logical "or" (|)Ander Biguri

1 Answers

1
votes

Your three criteria are the same as the one criteria:

  • Keep only rows with exactly 1 non-integer

Which, taking inspiration from your (very similar) question yesterday can be done using

B = A;
B(sum(mod(B,1)~=0, 2)~=1, :) = [];

This does the check for non-integers as seen in the previous question, then sums across the row to find rows where only one element passed. All other rows are removed.


If you actually wanted to implement it as 3 conditions, they can be done (in the order you wrote them) as logical "or" statements separated by a pipe |:

B = A;
B(sum(mod(B,1)~=0, 2)==2 | sum(mod(B,1)~=0, 2)==3 | sum(mod(B,1)==0, 2)==3, :) = [];