Search code examples
matlab

How do I get the number of rows that satisfy a condition?


If there's a matrix like this:

[20,39,50,80,21;
 40,79,80,65, 5;
 20,15,60,45,21;
 10,60,20,35,70];

How do I calculate the number of rows that have three or more elements which are less than 50?


Solution

    • Start with array: A
    • Check if elements are less than 50: b = A < 50
    • Number of columns per row where b is true: sum(b,2)
    • Number of rows where the above is 3 or more nnz(sum(b,2) >= 3)

    All together:

    numRowsWith3OrMoreValsUnder50 = nnz( sum(A<50, 2) >= 3 );