How to quickly find the position of a value in a sorted column data?

11 ビュー (過去 30 日間)
Leon
Leon 2018 年 3 月 14 日
コメント済み: Star Strider 2018 年 3 月 14 日
I have a list of ascendingly sorted data A = [-80, -79.6, -79.2, -77, 65,... 45]. If there is a value B, how do I quickly find the indices of the two values with one smaller than that and the very next one larger than that? For example, if B=79.8, the two indices will be 1 and 2. If B = 78, the two indices should be 3 and 4.
I know how to write a loop to process this, but my database is huge. As a result, it would take a huge amount of time to process.
Thanks!

採用された回答

Star Strider
Star Strider 2018 年 3 月 14 日
Try this:
V = linspace(-80, 45, 150); % Create Vector
B = -78;
idx = find(V <= B, 1, 'last');
Result = [idx idx+1];
  2 件のコメント
Leon
Leon 2018 年 3 月 14 日
Many thanks!
Star Strider
Star Strider 2018 年 3 月 14 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by