A bit hard to explain in the title. I have saved the output of hundreds of runs from a solver (from DifferentialEquations). The overall run time is the same between the runs (start and end time are the same), but the solver saves time steps (intervals) that make sense for it. This results in matrices being of different lengths.
I want to select rows in each matrix that is approximately at the same time value.
Here are three simplified matrices. I want to select the row in each matrix where the value of the first column (time) is as close to 2.0 as possible. This would be rows QWER[3,:]
, ASD[3,:]
and ZXC[2,:]
QWER = [0.0 1.0 2.0 3.0; 1.0 2.0 3.0 4.0; 2.0 3.0 4.0 5.0; 3.0 4.0 5.0 6.0]
ASD = [0.0 1.0 2.0 3.0; 0.5 2.0 3.0 4.0; 2.3 3.0 4.0 5.0; 2.5 3.0 4.0 5.0; 3.0 4.0 5.0 6.0]
ZXC = [0.0 1.0 2.0 3.0; 1.8 2.0 3.0 4.0; 3.0 3.0 4.0 5.0]
How could I do this?
If you want them all the same, just use saveat
?
I wasn’t sure when biomasses would stabilize from the different treatments so I let the solver choose the time step sizes. After running, I plot the biomasses over time and visually approximate when equilibrium actually happens.
Letting the solver choose the time steps is orthogonal to saveat
. saveat
lets the solver choose whatever time steps it wants, and then uses an internal interpolation to save at the required points. tstops
sets the solver times, not saveat
.
See the solver options page for details:
https://docs.sciml.ai/DiffEqDocs/stable/basics/common_solver_opts/
If you need to make this decision after the solve, you can use the interpolation yourself. For example, sol(0:0.1:1)
would re-evaluate the solution along t=0,0.1,0.2,… using the interpolation. Make sure you don’t use saveat
in that case to get a nonlinear interpolation. That is then a different answer here because it just means that the underlying matrix is adjustable in size because of its origin.
This is tricky for me as I run the code on a high-performance cluster, saving the outputs into JLD2 files and then pull those from the server. I can’t interpolate it myself from the state I get them.
Then I think saveat
would probably be the easiest way here.
last(findmin(abs.(QWER[:,1].-2)))
last(findmin(abs.(ASD[:,1].-2)))
last(findmin(abs.(ZXC[:,1].-2)))
or better
last(findmin(e->abs(e-2),QWER[:,1]))
# and similar expressions