# Storing output in matrix or vector format

**URL:** https://discourse.julialang.org/t/storing-output-in-matrix-or-vector-format/123207
**Category:** New to Julia
**Tags:** question
**Created:** [November 28, 2024, 10:32am UTC](https://discourse.julialang.org/t/storing-output-in-matrix-or-vector-format/123207 "2024-11-28T10:32:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![danieleavitabile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danieleavitabile/32/31966_2.png) [@danieleavitabile](https://discourse.julialang.org/u/danieleavitabile)
#### Post date: [November 28, 2024, 10:32am UTC](https://discourse.julialang.org/t/storing-output-in-matrix-or-vector-format/123207/1 "2024-11-28T10:32:39Z")

</div>

I’m new to Julia, coming from Matlab. Consider the following sketched function

```julia
function time_stepper(f,u0,nsteps, ...)

# Allocate arrays
t = zeros(nsteps)
p = length(u0)
u = zeros(p,nsteps +1)

# Initialise 
t[1] = 0.0
u[:,1] = u0

# update solution and update u, t

return t, u

```

While it’s clear that ` t` is a Vector, u may be a vector or a matrix, depending on `u0`. When p = 1, the code now returns a 1-column matrix, as opposed to a vector. Note I’ve used column-major structure for u, hoping this is good practice in Julia

What should I do to get a more idiomatic Julia code? What’s an appropriate data structure for `u` here?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 28, 2024, 10:50am UTC](https://discourse.julialang.org/t/storing-output-in-matrix-or-vector-format/123207/2 "2024-11-28T10:50:56Z")

</div>

Welcome from Matlab!

Your code is type stable regardless of the length of `u0`, it always returns a `Matrix`, which is p\times N if `u0` is a vector of length p (and if it’s 1\times N that would be similar to a _row_ vector.)

Now, if `u0` is a scalar instead of a length-1 vector, your code will fail with an error. In order to work for that situation too, you should add a dot to this expression: `u[:, 1] .= u0`, then it will work and be type stable in both cases.

If this is not your question, could you elaborate a bit more, for example giving two different example inputs with desired outputs?

(Then a small nitpick: Matlab doesn’t always force you to close a function definition with an `end`, but it is required in Julia. If you don’t do that, you will get errors or weird results.)

---

<div class="post-metadata">

### Author: ![danieleavitabile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danieleavitabile/32/31966_2.png) [@danieleavitabile](https://discourse.julialang.org/u/danieleavitabile)
#### Post date: [November 28, 2024, 11:02am UTC](https://discourse.julialang.org/t/storing-output-in-matrix-or-vector-format/123207/3 "2024-11-28T11:02:43Z")

</div>

Thanks @DNF, in fact, I threw ` u[:,1] .= u0` in the full code (and an end, but thanks for pointing this out!).

I’m wondering though if this is good Julia practice? I see that DifferentialEquation.jl, for instance, does not return a matrix for its output in a time stepper, but rather a vector of vectors? I don’t know if this makes sense, and thanks again

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [November 28, 2024, 11:19am UTC](https://discourse.julialang.org/t/storing-output-in-matrix-or-vector-format/123207/4 "2024-11-28T11:19:35Z")

</div>

Generally speaking, both a vector of vectors and the corresponding matrix can be good practice, depending on how it’s used and the performance characteristics of relevant operations.

---

<div class="post-metadata">

### Author: ![danieleavitabile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danieleavitabile/32/31966_2.png) [@danieleavitabile](https://discourse.julialang.org/u/danieleavitabile)
#### Post date: [November 28, 2024, 11:20am UTC](https://discourse.julialang.org/t/storing-output-in-matrix-or-vector-format/123207/5 "2024-11-28T11:20:55Z")

</div>

Thanks
