# Non-rectangular arrays in Julia

**URL:** <https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603>\
**Category:** New to Julia\
**Created:** [April 29, 2018, 7:39am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603 "2018-04-29T07:39:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [April 29, 2018, 7:39am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/1 "2018-04-29T07:39:44Z")

</div>

Hi,

I have to manipulate non-rectangular multidimensional arrays.  
Let `a` be a 2D array of floats with two columns and each column has a different size:  
`length(a[:,1])=n1` and `length(a[:,2])=n2` with `n1!=n2`

I wonder if it would be a good idea to create my own array type `MultiArray <: AbstractArray`  
but it would break the `AbstractArray` interface because the size method would not return a simple tuple…

Maybe a new `EvenMoreAbstractArray` with a `shape` function that returns a generalized form of size…

Any hint ?

---

<div class="post-metadata">

**Author:** ![felix](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felix/32/604_2.png) [@felix](https://discourse.julialang.org/u/felix)\
**Post date:** [April 29, 2018, 7:43am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/2 "2018-04-29T07:43:29Z")

</div>

Interesting, do you definitely need to represent it as a 2D array?

If yes, perhaps padding your data would be easier (you could pad it with garbage, and just record the lengths of the real data elsewhere).

If not, would an array of (variable-length) arrays work?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [April 29, 2018, 7:51am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/3 "2018-04-29T07:51:44Z")

</div>

Seems like [https://github.com/mbauman/RaggedArrays.jl](https://github.com/mbauman/RaggedArrays.jl) is what you are looking for. Might need to be updated for julia 0.6.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [April 29, 2018, 7:52am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/4 "2018-04-29T07:52:02Z")

</div>

Hi felix, thank you for this instantaneous reply !

The solution of an array of array is OK in my case (padding is not because the length can be very different).  
My only concerns is that the indexing will be inhomogeneous. If `a` is a 4D array built as a 2D array of (differently sized) 2D arrays, the indexing syntax will be:  
`a[i,j][k,l]`

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [April 29, 2018, 7:58am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/5 "2018-04-29T07:58:48Z")

</div>

Thank you for the RaggedArrays link !

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 29, 2018, 9:05am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/6 "2018-04-29T09:05:54Z")

</div>

I have a package for something similar:  
[https://github.com/tpapp/RaggedData.jl](https://github.com/tpapp/RaggedData.jl)

Also supports ingestion of data with an ex ante unknown number of elements per column.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [April 29, 2018, 9:55am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/7 "2018-04-29T09:55:01Z")

</div>

Thanks !  
actually for my application, I deal with ragged (I learned a new word) arrays of rectangular arrays.  
I need the inner rectangular arrays to be really fast. I guess that the first solution (array of arrays) will be more efficient…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 29, 2018, 10:00am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/8 "2018-04-29T10:00:19Z")

</div>

It depends. For my application, mapping into a flat vector was the most efficient (because it uses the least memory and I was memory-constrained, and I have lots of small vectors, with eg 5–100 elements). I think the same approach can be extended for arrays. But make sure your profile and benchmark. Also, I am experiencing a lot of speedups on `v0.7` compared to `v0.6`.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [April 29, 2018, 10:05am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/9 "2018-04-29T10:05:24Z")

</div>

Thank you for the tips. I will experiment the different options.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [April 29, 2018, 10:47am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/10 "2018-04-29T10:47:56Z")

</div>

Why not use an `Array{<:Array{<:Any,1},1}` for that?

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [April 29, 2018, 11:12am UTC](https://discourse.julialang.org/t/non-rectangular-arrays-in-julia/10603/11 "2018-04-29T11:12:18Z")

</div>

Yes, I guess that it is what felix proposed (array of array). I think I will go that way.
