Clear Filters
Clear Filters

How to fine a range of values before and after a specific value with a condition

5 views (last 30 days)
Hello,
Hope everyone is fine!
I have an issue regarding the data analysis in Matlab, if someone could support me.
I have a table of different columns (datetime, VAR1 and VAR2). I need to:
- first find a specific value of VAR1 (eg. X=5)
- then find a range of values of VAR2 before and after X, according to the values of VAR1 equal to 0.
I need to find something like that:
Datetime VAR1 VAR2
... 0 y1
... 0 y2
... 0 y3
... 0 y4
... 5 y5
... 0 y6
... 0 y7
... 0 y8
... 0 y9
  4 Comments
Voss
Voss on 25 Feb 2022
@Latifa EL BOUJDAINI: Can you have different number of rows before and after a 5? E.g., if you have 3 rows with 0 before the 5, and 6 rows with 0 after the 5, do you keep them all or do you just keep 3 before and 3 after?
Laty El
Laty El on 25 Feb 2022
@_ For my case i need to find many 0 rows before and after, and i have to use the same number of rows (before and after).

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 25 Feb 2022
Try this —
GetRange = @(range,Val,T,Col) strfind(T{:,Col}', [zeros(1,range) Val zeros(1,range)]);
Col2 = zeros(24,1);
Col2(randi(24,24,1)==5) = 5;
Table = table(datetime('now')+hours(0:23).', Col2, rand(24,1), 'VariableNames',{'Datetime','VAR1','VAR2'})
Table = 24×3 table
Datetime VAR1 VAR2 ____________________ ____ ________ 25-Feb-2022 14:16:05 0 0.94761 25-Feb-2022 15:16:05 0 0.66998 25-Feb-2022 16:16:05 0 0.80789 25-Feb-2022 17:16:05 0 0.39264 25-Feb-2022 18:16:05 0 0.28526 25-Feb-2022 19:16:05 0 0.29068 25-Feb-2022 20:16:05 0 0.44018 25-Feb-2022 21:16:05 0 0.85058 25-Feb-2022 22:16:05 5 0.50154 25-Feb-2022 23:16:05 0 0.11087 26-Feb-2022 00:16:05 5 0.68359 26-Feb-2022 01:16:05 0 0.80208 26-Feb-2022 02:16:05 0 0.053463 26-Feb-2022 03:16:05 0 0.98176 26-Feb-2022 04:16:05 0 0.69536 26-Feb-2022 05:16:05 0 0.61461
idx = find(Table.VAR1 == 5)
idx = 3×1
9 11 20
range = 3;
start = GetRange(3,5,Table,2)
start = 17
for k = 1:numel(start)
Out{k,:} = Table(start(k) : start(k)+range*2,:);
end
Out{:}
ans = 7×3 table
Datetime VAR1 VAR2 ____________________ ____ ________ 26-Feb-2022 06:16:05 0 0.11047 26-Feb-2022 07:16:05 0 0.11028 26-Feb-2022 08:16:05 0 0.81538 26-Feb-2022 09:16:05 5 0.96058 26-Feb-2022 10:16:05 0 0.97877 26-Feb-2022 11:16:05 0 0.025627 26-Feb-2022 12:16:05 0 0.48768
It will only detect and return regions with a centre value of 5 (in this example) and zeros within the specified range. So here, it does not return the values at 9 and 11 because they do not meet the criteria, and it does return the value with the centre value at 20 because it meets the criteria.
The ‘GetRange’ function arguments are ‘range’ the range to consider, here 3, ‘Val’ the value to compare (here 5), ‘T’ the table variable name (here ‘Table’), and ‘Col’ the column to test (here 2).
.
  18 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Trigonometry 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!