2
votes

What is the best way to delete rows of a matrix containing a number outside a specific range? For example

A =

200  400
500  200
500  100
600  200
200  100
300  200

Range = [200 500];

Rows 3,4 and 5 would then be deleted as they contain numbers <200 and >500.

2

2 Answers

6
votes

This should work for you -

A(any(A<200 | A>500,2),:)=[];

To state that generally -

range1 = [200 500]; %// changed the variable name as 
                    %// range is already a builtin function name
A(any(A<range1(1) | A>range1(2),2),:)=[];

If the number of rows to be deleted is a lot, for performance you might as well index into the other rows instead of deleting -

range1 = [200 500];
A = A(~any(A<range1(1) | A>range1(2),2),:)
0
votes

A simple solution could be:

A(any(A<200 | A>500,2),:) = [];