Effacer les filtres
Effacer les filtres

How to find vector components limiting a value?

1 vue (au cours des 30 derniers jours)
osminbas
osminbas le 2 Avr 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!

Réponse acceptée

the cyclist
the cyclist le 2 Avr 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.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by