MATLAB Answers

Finding a value, index which lies between 2 points in an array

3 views (last 30 days)
Yash Runwal
Yash Runwal on 10 Sep 2020
Commented: Yash Runwal on 10 Sep 2020
Hello fellow MATLAB users,
I am currently writing a MATLAB function for which I need some help.
For example:
I have an array and a value:
X = [0, 10, 20];
value = 17;
I need to find the indices of the X in which the value lies:
Here the answer will be X[2] and X[3].
But I am not sure how to do this. I would really appreciate any help I can get.
Thank You.

  0 Comments

Sign in to comment.

Accepted Answer

Matt J
Matt J on 10 Sep 2020
i=discretize(value,X)
j = i+1
X([i,j])

  1 Comment

Yash Runwal
Yash Runwal on 10 Sep 2020
Actually both of the answers work. I am gonna have to look up the functions discretize and interp1

Sign in to comment.

More Answers (1)

Matt J
Matt J on 10 Sep 2020
Edited: Matt J on 10 Sep 2020
i = interp1(X,1:numel(X),value,'previous')
j = i+1
X([i,j])

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!