# How to replace MATLAB cell array ({}) in Julia

**URL:** <https://discourse.julialang.org/t/how-to-replace-matlab-cell-array-in-julia/89690>\
**Category:** New to Julia\
**Created:** [November 3, 2022, 4:01am UTC](https://discourse.julialang.org/t/how-to-replace-matlab-cell-array-in-julia/89690 "2022-11-03T04:01:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSa](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@SSa](https://discourse.julialang.org/u/SSa)\
**Post date:** [November 3, 2022, 4:01am UTC](https://discourse.julialang.org/t/how-to-replace-matlab-cell-array-in-julia/89690/1 "2022-11-03T04:01:06Z")

</div>

This might be a very basic question. I have a MATLAB version iterative solver where I need to store my solution on each iteration. In MATLAB one can easily store by this sol{}. I was wondering what would be the equivalence cell structure in Julia?

I know Array could be one approach to store the solution but I am having a hard time to initialize the store variable inside the loop.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 3, 2022, 4:16am UTC](https://discourse.julialang.org/t/how-to-replace-matlab-cell-array-in-julia/89690/2 "2022-11-03T04:16:36Z")

</div>

make an empty array and `push!` into it.

---

<div class="post-metadata">

**Author:** ![SSa](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@SSa](https://discourse.julialang.org/u/SSa)\
**Post date:** [November 3, 2022, 1:48pm UTC](https://discourse.julialang.org/t/how-to-replace-matlab-cell-array-in-julia/89690/3 "2022-11-03T13:48:22Z")

</div>

Thank you Oscar.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 3, 2022, 2:37pm UTC](https://discourse.julialang.org/t/how-to-replace-matlab-cell-array-in-julia/89690/4 "2022-11-03T14:37:04Z")

</div>

> [@SSa](#):
>
> I was wondering what would be the equivalence cell structure in Julia?

The direct equivalent of a cell array is a `Vector{Any}` in Julia (or `Array{Any,N}` for an `N`-dimensional cell array), i.e. an array which can hold any type of data. You can initialize this to an empty array with `a = Any[]` or just `a = []`, and then append values with `push!(a, v)`.

However, to gain the full performance benefits of Julia one usually [avoids abstractly typed containers](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container), i.e. we usually try to give all our arrays concrete element types if we know the type of data in advance. For example, if you know that you want to store 1d arrays `Vector{Float64}` of floating-point data, you can do:

```julia
a = Vector{Float64}[] # an empty container for Vector{Float64}
push!(a, x) # store an array x in a

```

Better yet, you can try to write “type-generic” code where the element type of `a` is determined by the inputs to your computation. For example, in an iterative solver, you typically have an initial guess `x0`, and all subsequent iterations are of the same type. So in this case you can initialize `a` to hold arrays of the same type as `x0`:

```julia
a = typeof(x0)[] # empty array to hold objects of the same type as x0

```

This gives you maximum flexibility/generality — e.g. you can run the same code with vectors of single-precision (`Float32`) or complex (`Complex{Float64}`) data, and it will work (and be fast) without modification. In the long run, one tries to get in the habit of writing type-generic Julia code. But to start with you can specialize everything for e.g. `Vector{Float64}` and go back and generalize it later.

---

<div class="post-metadata">

**Author:** ![SSa](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@SSa](https://discourse.julialang.org/u/SSa)\
**Post date:** [November 3, 2022, 2:52pm UTC](https://discourse.julialang.org/t/how-to-replace-matlab-cell-array-in-julia/89690/5 "2022-11-03T14:52:07Z")

</div>

Thank you Steven for your nice explanation.

---

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [November 3, 2022, 7:33pm UTC](https://discourse.julialang.org/t/how-to-replace-matlab-cell-array-in-julia/89690/6 "2022-11-03T19:33:21Z")

</div>

This may also be of interest:  
[https://omlins.github.io/CellArrays.jl/dev/](https://omlins.github.io/CellArrays.jl/dev/)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 3, 2022, 8:32pm UTC](https://discourse.julialang.org/t/how-to-replace-matlab-cell-array-in-julia/89690/7 "2022-11-03T20:32:51Z")

</div>

I think that’s quite a different meaning of “cell array” what is meant in Matlab.
