Q&A
Ask and answer questions to make information more available to wider audiences.
Colton Holybees @coltonholybees   18, Aug 2023 12:00 AM
Select only a specific number of rows fulfilling a condition in MATLAB
Select only a specific number of rows fulfilling a condition in MATLAB
Assume you have the following data matrix:
A =

    1   11   22   33
   44   13   12   33
    1   14   33   44
Delete all rows of this matrix that don't accomplish the condition A(:, 4) == 33 and get the matrix of this form which only selects these rows:

  A_new =

   1   11   22   33
  44   13   12   33

answers 1
 
Answer 1
Chance Boorman @boormanchance   31, Aug 2023 10:34 AM
Using logical indexing, this can be achieved as follows

A = [
    1   11   22   33
    44  13   12   33
    1   14   33   44
];
idx = ( A(:,4)==33 );
A_new = A(idx,:)

Output:

A_new =

     1    11    22    33
    44    13    12    33