0

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!

User1772
  • 91
  • 8
  • Do you know how to remove columns according to one criteria? Just do the same, but use the logical "or" (`|`) – Ander Biguri May 16 '17 at 11:11
  • Possible duplicate of [How do i remove matrix rows that contain all integers?](http://stackoverflow.com/questions/43983940/how-do-i-remove-matrix-rows-that-contain-all-integers) – Wolfie May 16 '17 at 11:19

1 Answers1

1

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, :) = [];
Community
  • 1
  • 1
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • OP might not want to overwrite `A`, although, it is simple to change to `B=A(sum(mod(A,1)~=0,2)==1,:);`. – JacobD May 16 '17 at 11:32
  • @JacobD I was just mimicking the OP's accepted answer from the linked question. I had missed that the answer first assigned `B` as an output matrix, I've edited my answer accordingly. – Wolfie May 16 '17 at 11:36
  • Thanks @Wolfie and thanks everyone. One more thing i would have wanted to ask (I know i should probably ask a new question, but i could update this one if need be) is how to obtain a `17x1 matrix` from `B` which has the `count of the rows of B` numbered from `1-17`. Please any ideas how i could try? Thanks once again! – User1772 May 16 '17 at 11:47
  • @User1772 You mean the list of row numbers of `A` which meet your criteria (and are seen in `B`)? – Wolfie May 16 '17 at 11:48
  • No! sorry, its just a list of row numbers in B. This has no relationship with A. I just want the rows in B counted. I would need this count to extract the x,y,z coordinate of any of the rows at some point in my code. Sorry about the mix-up. Thank you! – User1772 May 16 '17 at 11:57
  • You can get the number of rows using `numrows = size(B, 1)`, then get a list `1,2,3,...,numrows` using `rownumbers = 1:numrows`. – Wolfie May 16 '17 at 13:01