Reputation: 1271
If i've got a matrix which is something like:
400 450 450 300
450 300 450 340
450 200 300 210
300 100 300 200
200 200 100 100
475 300 225 300
What is the best way to delete all columns which do not contain numbers between a specific range - for example 400-500 - at least twice or more. In this example, columns B and D will be removed as B contains a number in this range only once and D contains nothing within this range.
The desired output would be:
400 450
450 450
450 300
300 300
200 100
475 225
I've tried to use an IF statement to scan each column and move matches into a new matrix but this seems inefficient.
Upvotes: 1
Views: 37
Reputation: 221714
Assuming A
to be the input matrix, you can use this matrix indexing approach -
A(:,sum(A>=400 & A<=450,1)>=2)
This must be a pretty efficient approach to your problem.
sum(A>=400 & A<=450,1)>=2
gets us a logical array of columns that satisfy our needs and then we index into the columns of A
with it.
Note: If the number of columns to be removed is a very small number as compared to the total number of columns, you can directly remove them instead of indexing into the matched columns for a more efficient approach in that special situation. So, in that case you can do this -
A(:,sum(A>=400 & A<=450,1)<2)=[];
Upvotes: 1