Question

I have a matrix A = [1 2 3;2 5 9;2 3 4]. Now I want to make a search on all the elements of the matrix. Any element found greater than 8 should be detected and whole row pertaining to that element should be deleted. As in this example A(2,3)>8. Hence in the final output matrix row 2 should be deleted and the output matrix be B = [1,2,3;2,3,4]

Was it helpful?

Solution

The inverse of Shai's answer is usually faster in loops:

B = A( all(A<=8,2), : );

or

B = A( all(A<9,2), : );

if you desire.

Note that this may not be true on newer Matlab versions (R2012a I believe has specific JIT optimizations for loops with matrix deletions). Nevertheless it's a safer bet, and may be more intuitive.

OTHER TIPS

use logical indexing and any command

>> selRowToDelete = any( A > 8, 2 ); % any value on dim 2 (rows)
>> A( selRowToDelete, : ) = []; % remove the rows
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top