How to find the last position in an array where a value is met
97 views (last 30 days)
Show older comments
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).
0 Comments
Answers (1)
Walter Roberson
on 20 Dec 2016
Edited: Walter Roberson
on 20 Dec 2016
find(y >= 0.98, 1, 'last')
Or for your expanded requirement,
find(y >= 0.98 & y <= 1.02, 1, 'last')
3 Comments
Walter Roberson
on 20 Dec 2016
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
on 21 Sep 2023
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');
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!