# DifferentialEquations & DynamicalSystems: Converting output Vector{Vector{Float64}} to StateSpaceSet

**URL:** <https://discourse.julialang.org/t/differentialequations-dynamicalsystems-converting-output-vector-vector-float64-to-statespaceset/105793>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [November 4, 2023, 1:25pm UTC](https://discourse.julialang.org/t/differentialequations-dynamicalsystems-converting-output-vector-vector-float64-to-statespaceset/105793 "2023-11-04T13:25:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MvanSteenbergen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvansteenbergen/32/202866_2.png) [@MvanSteenbergen](https://discourse.julialang.org/u/MvanSteenbergen)\
**Post date:** [November 4, 2023, 1:25pm UTC](https://discourse.julialang.org/t/differentialequations-dynamicalsystems-converting-output-vector-vector-float64-to-statespaceset/105793/1 "2023-11-04T13:25:59Z")

</div>

I edited the post and added my steps below

Hey,

I am having compatibility issues with the DynamicalSystems and DifferentialEquations packages.

I have implemented the following rule set using the DifferentialEquations package:

```julia
using DifferentialEquations
using Distributions
using DynamicalSystems
using Random

function threePlusOneDimensions!(du, u, p, t)
    Smax, Rₛ, λₛ, τx, P, Rᵦ, λᵦ, L, τᵧ, S, α, β, τz, ζ, λf, τf = p

    du[1] = (Smax /(1 + exp((Rₛ - u[2]) / λₛ)) - u[1]) / τx
    du[2] = (P / (1 + exp((Rᵦ - u[2]) / λᵦ)) + u[4] * L - u[1] * u[2] - u[3]) / τᵧ
    du[3] = (S * (α * u[1] + β * u[2]) * p[14] - u[3]) / τz
    du[4] = (u[2] - λf * u[4]) / τf
    return nothing
end 

function affect!(integrator)
    integrator.p[14] = clamp(rand(Normal(0, 0.5)), -1.0, 1.0)
    return nothing
end

cb = PeriodicCallback(affect!, 0.01)

p₀ = [10, 1, 0.1, 14, 10, 1.04, 0.05, 0.2, 14, 4, 0.5, 0.5, 1, 0.2, 1, 720]
u₀ = [0.0, 0.01, 0.0, 0.0]

prob = ODEProblem(threePlusOneDimensions!, u₀, (0.0,12000.0), p₀)
X = solve(prob, Tsit5(), callback=cb, reltol = 1e-9, abstol = 1e-9, saveat = 0.01)

```

I would like to calculate recurrence statistics on the basis of the trajectory. I have tried implementing this using the `trajectory` function of the DynamicalSystems package, but I can’t figure out how to use the `callback` argument with it, if it even supports it. I am now using `solve()` instead, but this outputs the trajectory I need as a `Vector{Vector{Float64}}`, while RecurrenceMatrix requires a `StateSpaceSet`. How can I convert the `Vector{Vector{Float64}}` to a `StateSpaceSet`, so that I can calculate the recurrence statistics?

**EDIT:**  
I used the keyword ε explicity for the RecurrenceMatrix function using a symbol copied from the internet, and that’s why it gave the type error (from the RecurrenceMatrix documentation). I tried using similar code @empet gave before, but then it gave a different type error because of the ε-sign. I did not notice this, assuming it was the same error as I mentioned before.

I solved it using the next snippet of code:

```julia
matrix = StateSpaceSet(healthy[:,:]')

RecurrenceMatrix(matrix[1:100], 0.1)

```

where before it was

```julia
RecurrenceMatrix(matrix[1:100], ε = 0.1)

```

instead of that last line.

Then, it works. I was wondering! I heard all this jazz about multiple dispatch. Would it be possible to add a method to RecurrenceMatrix so that it directly works on ODESolutions, so that it converts the type for you?

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [November 4, 2023, 4:40pm UTC](https://discourse.julialang.org/t/differentialequations-dynamicalsystems-converting-output-vector-vector-float64-to-statespaceset/105793/2 "2023-11-04T16:40:19Z")

</div>

Convert the vector of vectors to a matrix, having as rows the vectors. Below it is the transpose, `matrix'`, that fulfills this condition. Then  
the last line of code converts `matrix'` to a `StateSpaceSet`.

```julia
vecofvecs = [rand(3) for _ in 1:5]
matrix = hcat(vecofvecs...)
ssset = StateSpaceSet(matrix')

```

---

<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:** [November 6, 2023, 3:06am UTC](https://discourse.julialang.org/t/differentialequations-dynamicalsystems-converting-output-vector-vector-float64-to-statespaceset/105793/3 "2023-11-06T03:06:34Z")

</div>

Make life simple 😅 . It’s already represented as a matrix in indexing. Just call `Matrix(X')` or whatnot.
