Efficient alternative to find()

5 次查看(过去 30 天)
I would like to see if there is any more efficient alternative to find() for this problem:
Let's say my x-values are x0= [1 ,1.1, 1.3, 1.5 ,1.6, 1.7] and the corresponding y-values are [ 2, 3 ,4 ,7, 9 ,11], i.e y is a "function" of x.
Suppose I have a new set of x-values given by x1 = [1.1, 1.12, 1.25, 1.55, 1.65,1.68].
I want to create a new vector y1 with y1(i) being equal to the value in the original y such that its corresponding x0(i) is the last element in x0 that is smaller than x1(i).
For this case, the vector y1 should be [3,3,3,7,9,9]. For example, y1(4) is equal to 7 because 1.5 is the last element in x0 that is smaller than 1.55.
I tried doing this with find() and loop over all vector entries but it turns out to be rather slow. Any advice on more efficient implementations would be appreciated.
Edit: My original example had a mistake

采纳的回答

Star Strider
Star Strider 2023-8-13
The interp1 function with the 'previous' interpolation method may be appropriate here —
x0= [1 ,1.1, 1.3, 1.5 ,1.6, 1.7];
y0 = [ 2, 3 ,4 ,7, 9 ,11];
x1 = [1.1, 1.12, 1.25, 1.55, 1.65,1.68];
y1 = interp1(x0, y0, x1, 'previous')
y1 = 1×6
3 3 3 7 9 9
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by