# How to select rows of differently sized matrices based on an approximate value within a column of each matrix

**URL:** <https://discourse.julialang.org/t/how-to-select-rows-of-differently-sized-matrices-based-on-an-approximate-value-within-a-column-of-each-matrix/100343>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 14, 2023, 4:23pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-differently-sized-matrices-based-on-an-approximate-value-within-a-column-of-each-matrix/100343 "2023-06-14T16:23:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [June 14, 2023, 4:23pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-differently-sized-matrices-based-on-an-approximate-value-within-a-column-of-each-matrix/100343/1 "2023-06-14T16:23:17Z")

</div>

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,:]`

```julia
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?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 14, 2023, 4:26pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-differently-sized-matrices-based-on-an-approximate-value-within-a-column-of-each-matrix/100343/2 "2023-06-14T16:26:26Z")

</div>

> [@HelgavonLichtenstein](#):
>
> 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.

If you want them all the same, just use `saveat`?

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [June 14, 2023, 4:32pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-differently-sized-matrices-based-on-an-approximate-value-within-a-column-of-each-matrix/100343/3 "2023-06-14T16:32:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 14, 2023, 4:38pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-differently-sized-matrices-based-on-an-approximate-value-within-a-column-of-each-matrix/100343/4 "2023-06-14T16:38:17Z")

</div>

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/](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.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [June 14, 2023, 4:47pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-differently-sized-matrices-based-on-an-approximate-value-within-a-column-of-each-matrix/100343/5 "2023-06-14T16:47:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 14, 2023, 4:52pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-differently-sized-matrices-based-on-an-approximate-value-within-a-column-of-each-matrix/100343/6 "2023-06-14T16:52:59Z")

</div>

Then I think `saveat` would probably be the easiest way here.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 14, 2023, 7:27pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-differently-sized-matrices-based-on-an-approximate-value-within-a-column-of-each-matrix/100343/7 "2023-06-14T19:27:35Z")

</div>

```julia
last(findmin(abs.(QWER[:,1].-2)))
last(findmin(abs.(ASD[:,1].-2)))
last(findmin(abs.(ZXC[:,1].-2)))

```

or better

```julia
last(findmin(e->abs(e-2),QWER[:,1]))
# and similar expressions

```
