First element greater than

29 ビュー (過去 30 日間)
AAZ
AAZ 2022 年 8 月 17 日
コメント済み: Star Strider 2022 年 8 月 17 日
I have a set of data containing x,
How can i extract only the first value greater than x in this set.

採用された回答

Star Strider
Star Strider 2022 年 8 月 17 日
Try simething like this —
v = randi(9, 1, 20)
v = 1×20
5 4 2 5 2 6 7 2 6 6 3 8 5 8 7 4 7 4 8 7
x = 7
x = 7
first_x_idx = find(v > x,1,'first')
first_x_idx = 12
Result = v(first_x_idx)
Result = 8
.
  2 件のコメント
AAZ
AAZ 2022 年 8 月 17 日
I have v=[-0.1011 -0.0712 -0.0562] , and x=-0.0599, so i try to use the code above but it give for the first value lower than x is -0.1011 but i want -0.0721.
So how can i solve it
Star Strider
Star Strider 2022 年 8 月 17 日
I am confused. I am not certain what you want.
I am not certain how robust this will be for other vectors, however here it gives the result you want —
v = [-0.1011 -0.0712 -0.0562];
x = -0.0599;
first_x_idx = interp1(v,(1:numel(v)),x,'previous')
first_x_idx = 2
Result = v(first_x_idx)
Result = -0.0712
.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by