Filter löschen
Filter löschen

How to find vector components limiting a value?

1 Ansicht (letzte 30 Tage)
osminbas
osminbas am 2 Apr. 2012
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!

Akzeptierte Antwort

the cyclist
the cyclist am 2 Apr. 2012
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.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by