Find data between ranges in matlab

I have a fairly simplified example that I would like to study for the best possible solution. I have a dataset:

depth = [0:0.5:20]; 

I only want to select the "depth" from a certain range, for example from 2 to 5. I can do this with:

 d1 = find(depth == 2,1,'first'); d2 = find(depth == 5,1,'first'); depth = depth(d1:d2); 

Is there an alternative, cleaner way to do this?

+7
source share
1 answer

just use boolean indexing:

  depth(depth >= 2 & depth <= 5) 
+12
source

All Articles