trying to output the indeces for plus minus 5 from each value in an array
2 views (last 30 days)
Show older comments
I am trying to create a loop that will find the index values for each value in an array plus minus 5
so far this is what I have:
i have an array of times from 100 to 3000 that goes up by icnrements of 100, but i want to find the indeces for 95 to 105, 195 to 205, 295 to 305, etc
I was able to do it for one value, but I dont want to have to manually out in the parameters for all 30 timepoints
0 Comments
Accepted Answer
Voss
on 15 Jun 2022
Edited: Voss
on 15 Jun 2022
If you compare a column vector to a row vector, you get a matrix that is the result of comparing each element of one vector to all elements of the other vector. Then you can use find on that matrix to get the row and column indices where the comparison condition is true. For instance:
a = [1; 2; 3; 4]; % column vector, 4x1
b = [1 2 3]; % row vector, 1x3
a < b % matrix, 4x3
[a_idx,b_idx] = find(a < b)
Applying that same idea to your situation:
time = sort(3000*rand(1,1500)); % some random times called "time"
times = 100:100:3000;
lowrange = times-5;
upperrange = times+5;
[time_idx,times_idx] = find(time(:) > lowrange & time(:) < upperrange)
disp([time(time_idx); times(times_idx)].')
2 Comments
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays 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!