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]
Asked
Active
Viewed 210 times
2 Answers
1
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.

Rody Oldenhuis
- 37,726
- 7
- 50
- 96
0
use logical indexing and any
command
>> selRowToDelete = any( A > 8, 2 ); % any value on dim 2 (rows)
>> A( selRowToDelete, : ) = []; % remove the rows

Dennis Jaheruddin
- 21,208
- 8
- 66
- 122

Shai
- 111,146
- 38
- 238
- 371