How to find vector components limiting a value?

2 次查看(过去 30 天)
Hello,
Say A=[2;4;7;9;11;15].
How do you find the components (indices) of A which are limiting a value, say k=10?
In this case, it is easy to see that A(4) and A(5) are the limiting components, i.e, k is in between 9 and 11: A(4)<k<A(5). My actual A is much bigger.
Thanks!

采纳的回答

the cyclist
the cyclist 2012-4-2
If your vector is guaranteed to be in sorted order, as in your example, then
max(find(a<k))
and
min(find(a>k))
will give the two limits.
You might want to make one of those include the equality, in case it might be exactly equal to the border. Also, you really only need to calculate, say, the lower limit, and then the other limit is just the next index.
Finally, you might need to consider what to do if all values in the vector are above (or below) the value of k.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by