Finding the position of specific value in a vector without knowing the exact number

7 views (last 30 days)
I have a vector about the time of an experiment. Now, I want a specific piece of information that was inbetween 3600 and 3800 seconds. However, the measuring points and thus the row numbers, do not match the seconds. So for example, 3600 seconds is the 3482 measuring point/row, but this also changes with my samples.
I thought of finding the correct rows that I want the information of by searching for 3600 and 3800 in the vector. However, there is not a point exactly at 3600 and 3800. I therefore added:
Test = find(abs(Time-3600) <1)
However, this also finds the numbers that are for example 3599. Is there a way in which I can find the number above 3600 closest to 3600. So for example 3600.8, while leaving 3599 out?
  2 Comments
Sargondjani
Sargondjani on 21 Jan 2021
Edited: Sargondjani on 21 Jan 2021
If your data is sorted then you could try to find the first entry larger than 3600: tt = find(Time>3600,1). This will find the first entry larger than 3600. (alternatively you could first sort the data).
Otherwise it is a bit more difficult. It is similar to this question (minium + condition):

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 21 Jan 2021
The find documentation section Last Several Nonzero Elements will let you return the indices of only some of the elements. You can then choose the ones you want.

Steven Lord
Steven Lord on 21 Jan 2021
Depending on how you're storing your data and what analysis / manipulation you want to perform on it, storing it in a timetable array and indexing into the rows of the timetable using a timerange may be useful.
n = datetime('now');
t = n + minutes(randi([-5 5], 10, 1));
x = (1:10).'.^2;
tt = timetable(t, x)
tt = 10x1 timetable
t x ____________________ ___ 21-Jan-2021 17:02:40 1 21-Jan-2021 17:09:40 4 21-Jan-2021 17:10:40 9 21-Jan-2021 17:01:40 16 21-Jan-2021 17:08:40 25 21-Jan-2021 17:02:40 36 21-Jan-2021 17:03:40 49 21-Jan-2021 17:04:40 64 21-Jan-2021 17:07:40 81 21-Jan-2021 17:09:40 100
r = timerange(n-minutes(2), n+minutes(3), 'closed')
r =
timetable timerange subscript: Select timetable rows with times in the closed interval: [21-Jan-2021 17:04:40, 21-Jan-2021 17:09:40] See Select Timetable Data by Row Time and Variable Type.
tt(r, :)
ans = 5x1 timetable
t x ____________________ ___ 21-Jan-2021 17:09:40 4 21-Jan-2021 17:08:40 25 21-Jan-2021 17:04:40 64 21-Jan-2021 17:07:40 81 21-Jan-2021 17:09:40 100

Categories

Find more on Preprocessing Data in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!