How to find the last position in an array where a value is met

96 ビュー (過去 30 日間)
Robert Marasescu
Robert Marasescu 2016 年 12 月 20 日
コメント済み: Tanner DeKruyter 2023 年 9 月 21 日
I have an array of about 100 elements, all random generated by a function and I need to find the last position where the value 0.98 is met. Also, the values are with more than 6 decimals so they need to be approximated to just 2, but I can do that with round function.
Ox axis is an array that contains time values, and Oy axis is an array which contains the values of the function. I have to find the last value on Oy where y (the function) is + or - 2% of 1 (which is 0.98 or 1.02).

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 12 月 20 日
編集済み: Walter Roberson 2016 年 12 月 20 日
find(y >= 0.98, 1, 'last')
Or for your expanded requirement,
find(y >= 0.98 & y <= 1.02, 1, 'last')
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 12 月 20 日
What is the value of y(end) ? If the plot you included represents the data, then the value at the right hand side is between 0.98 and 1.02: visually we can see that the value is quite close to 1.0
Tanner DeKruyter
Tanner DeKruyter 2023 年 9 月 21 日
I know this is a bit late, but I'm in controls right now and just did this. You need to reverse the bounds to search outside the 2% from steady state. My code was instead:
ts_idx = find(or(y1 <= 0.98*yss, y1 >= 1.02*yss), 1, 'last');

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by