trying to output the indeces for plus minus 5 from each value in an array

2 views (last 30 days)
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

Accepted Answer

Voss
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
ans = 4×3 logical array
0 1 1 0 0 1 0 0 0 0 0 0
[a_idx,b_idx] = find(a < b)
a_idx = 3×1
1 1 2
b_idx = 3×1
2 3 3
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)
time_idx = 142×1
51 52 53 54 55 56 57 103 104 143
times_idx = 142×1
1 1 1 1 1 1 1 2 2 3
disp([time(time_idx); times(times_idx)].')
1.0e+03 * 0.0956 0.1000 0.0963 0.1000 0.0994 0.1000 0.0995 0.1000 0.1000 0.1000 0.1004 0.1000 0.1022 0.1000 0.2021 0.2000 0.2025 0.2000 0.2983 0.3000 0.3000 0.3000 0.3002 0.3000 0.3008 0.3000 0.3010 0.3000 0.3010 0.3000 0.3026 0.3000 0.3027 0.3000 0.3043 0.3000 0.3044 0.3000 0.3951 0.4000 0.3957 0.4000 0.3961 0.4000 0.3961 0.4000 0.3973 0.4000 0.3993 0.4000 0.4002 0.4000 0.4009 0.4000 0.4010 0.4000 0.4022 0.4000 0.4964 0.5000 0.4978 0.5000 0.4995 0.5000 0.5953 0.6000 0.5964 0.6000 0.5965 0.6000 0.5974 0.6000 0.5978 0.6000 0.5982 0.6000 0.6010 0.6000 0.6018 0.6000 0.6030 0.6000 0.6038 0.6000 0.6954 0.7000 0.6994 0.7000 0.7966 0.8000 0.7970 0.8000 0.8038 0.8000 0.8049 0.8000 0.8966 0.9000 0.8977 0.9000 0.8984 0.9000 0.8991 0.9000 0.8992 0.9000 0.8997 0.9000 0.8999 0.9000 0.9013 0.9000 0.9972 1.0000 0.9983 1.0000 1.0003 1.0000 1.0020 1.0000 1.0045 1.0000 1.0048 1.0000 1.0964 1.1000 1.0989 1.1000 1.1006 1.1000 1.1982 1.2000 1.1991 1.2000 1.2015 1.2000 1.2040 1.2000 1.2973 1.3000 1.3004 1.3000 1.3007 1.3000 1.3991 1.4000 1.4007 1.4000 1.4027 1.4000 1.4042 1.4000 1.4954 1.5000 1.4980 1.5000 1.4984 1.5000 1.4989 1.5000 1.5004 1.5000 1.5032 1.5000 1.5964 1.6000 1.5965 1.6000 1.5977 1.6000 1.5998 1.6000 1.6013 1.6000 1.6033 1.6000 1.6966 1.7000 1.6968 1.7000 1.6998 1.7000 1.7019 1.7000 1.8014 1.8000 1.8033 1.8000 1.8990 1.9000 1.9013 1.9000 1.9029 1.9000 1.9032 1.9000 1.9967 2.0000 1.9991 2.0000 1.9995 2.0000 2.0012 2.0000 2.0012 2.0000 2.0017 2.0000 2.0033 2.0000 2.0952 2.1000 2.1965 2.2000 2.2021 2.2000 2.2044 2.2000 2.2950 2.3000 2.2964 2.3000 2.3015 2.3000 2.3036 2.3000 2.3958 2.4000 2.3986 2.4000 2.4015 2.4000 2.4021 2.4000 2.4026 2.4000 2.4030 2.4000 2.4041 2.4000 2.4959 2.5000 2.5043 2.5000 2.5046 2.5000 2.5962 2.6000 2.5993 2.6000 2.5994 2.6000 2.6015 2.6000 2.6037 2.6000 2.6039 2.6000 2.6954 2.7000 2.6999 2.7000 2.7023 2.7000 2.7034 2.7000 2.8010 2.8000 2.8953 2.9000 2.8989 2.9000 2.9037 2.9000 2.9046 2.9000 2.9048 2.9000 2.9957 3.0000 2.9968 3.0000 2.9989 3.0000

More Answers (0)

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!