Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find data between range in matlab

I have a rather simplistic example that I would like to learn the best possible solution for. I have a data set:

depth = [0:0.5:20];

I want to only select 'depth' from a specific range, for example from 2 to 5. I can do this by:

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

Is there an alternative, cleaner way of doing this?

like image 248
KatyB Avatar asked Nov 06 '12 10:11

KatyB


People also ask

How do you find the range in Matlab?

y = range( X ,'all') returns the range of all elements in X . y = range( X , dim ) returns the range along the operating dimension dim of X . For example, if X is a matrix, then range(X,2) is a column vector containing the range value of each row.

How do you find the number of elements in an array in Matlab?

n = numel( A ) returns the number of elements, n , in array A , equivalent to prod(size(A)) .


1 Answers

just use logical indexing:

 depth(depth >= 2 & depth <= 5)
like image 140
Rody Oldenhuis Avatar answered Oct 10 '22 15:10

Rody Oldenhuis