# Find locations of a value in 3-D matrix (lat x lon x time) and take the two time steps before and after those selected values

2 views (last 30 days)
James Karden on 9 May 2023
Answered: Eric Sofen on 5 Jun 2023
Hello,
I am trying to filter out my dataset using another dataset. I have one dataset made up of 1's (lightning occured) and 0's (no lightning) telling me when lightning strike occur at a given lat x lon point. The data is 3-D (lat x lon x time). I have another dataset of precipitation values that is the same dimensions (lat x lon x time).
My goal is to look at these storms and see how much precipitation occurs during them over a specific area. Right now, my code finds when lightning strikes (ls =1) in my area, and then I multiply the lightning data by my precipitation data and get a precipitation value for that time step.
This works great, however I am looking to also select the few time steps before and the few after the lightning strikes to gather that precipitation data as well. Currently, I am only getting the precipitation values at the time the lightning occurs, not through the whole storm.
Is it possible to find times of lightning strikes, and take the two hours (timesteps) before and after is occurs in my region?
Variables:
lightning strikes (ls) - lat x lon x time (316 x 332 x 2160 hours)
precipitation (precip) - lat x lon x time (316 x 332 x 2160 hours)
my_study_region - lat x lon (316 x 332)
Thank you,
James

Matt J on 9 May 2023
Edited: Matt J on 9 May 2023
One way:
sz=size(ls);
[I,J,K]=ind2sub(sz, find(ls) );
A=precip(sub2ind(sz,I,J,K-2)); %2 hours before
B=precip(sub2ind(sz,I,J,K-1)); %1 hours before
C=precip(sub2ind(sz,I,J,K-0)); %0 hours before
D=precip(sub2ind(sz,I,J,K+1)); %1 hours after
E=precip(sub2ind(sz,I,J,K+2)); %2 hours after

Eric Sofen on 5 Jun 2023
Since you're using lighting as a logical mask on the precipitation data, you can shift the array one or two time steps and re-apply it. You just need to pad the other end of the time dimension so that dimensions match. For example, in 2D (say lat x time):
precip = rand(5)
precip = 5×5
0.9416 0.9428 0.1510 0.3523 0.7655 0.8665 0.9759 0.2552 0.3048 0.5998 0.9607 0.3125 0.5625 0.2300 0.1663 0.2071 0.0793 0.1410 0.3814 0.1150 0.4297 0.6957 0.0180 0.6849 0.7946
lightning = rand(5) > 0.8; % just to get a few 1s in an array of 0s.
zeros(1,5)
ans = 1×5
0 0 0 0 0
lightning(1:4,:)
ans = 4×5 logical array
0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
lag1lightning = [zeros(1,5); lightning(1:4,:)] % in general in 3d, this would be something like..
lag1lightning = 5×5
0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
% sz = size(lightining);
% lag1lighting = cat(3, zeros(sz(1:2)), lightning(:,:,1:end-1));
0 0 0 0 1 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 1 0 0 1 0
inStorm = 7×1
0.9607 0.2071 0.4297 0.6849 0.7655 0.5998 0.1663

### Categories

Find more on Coordinate Reference Systems 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!