Find rows with a specific value in their columns

21,920

if your matrix is called A, just do:

A(A(:,7)==1,:)

PS: I think that this question has already been answered a million times...

Share:
21,920
Sam
Author by

Sam

Updated on September 12, 2020

Comments

  • Sam
    Sam over 3 years

    Possible Duplicate:
    Find rows in matrix where entries match certain constraints?

    In Matlab, i have a matrix (MxN) and I want to find the rows whose entry in a specific column is equal to a specified value. For example, I have a matrix as follow:

       0    0   0   0   0   0   0   0   1   0
       1    0   0   0   0   0   1   0   0   1
       0    0   1   0   0   0   0   0   0   0
       0    0   0   1   0   0   0   0   1   0
       0    0   0   0   1   0   0   0   0   1
       0    0   0   0   0   0   0   0   0   0
       0    1   1   0   0   0   1   0   1   0
       0    0   0   0   0   0   0   0   0   1
       0    0   0   0   0   0   0   0   0   0
       0    0   0   0   0   0   0   0   0   1
    

    I want to find the rows whose 7th element us equal to 1. In this example, the result matrix should contains rows 2 and 7.

  • Sam
    Sam over 11 years
    tnx Oli, I know it was a silly question, but for a moment I was confused with a big matrix! :(
  • Sam
    Sam over 11 years
    @ Oli: another question: how I should change your code if I want to select a column (or columns) that is/are between for example a and b? I mean, not an exact value, but a range
  • Sam
    Sam over 11 years
    found it, I used && instead of &!!