# Problem Adding a dimesion to array/vector

**URL:** <https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431>\
**Category:** General Usage\
**Tags:** question, arrays, tullio, splitapplycombine\
**Created:** [December 7, 2020, 11:30pm UTC](https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431 "2020-12-07T23:30:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorrp1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lorrp1/32/17647_2.png) [@lorrp1](https://discourse.julialang.org/u/lorrp1)\
**Post date:** [December 7, 2020, 11:30pm UTC](https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431/1 "2020-12-07T23:30:48Z")

</div>

Hello, i have a  
`Vector{Any} with 25701 elements made of: 100×40 Array{Float64,2}`

id like to add a dimension to it so from `25701 * 100 * 40*1`

i have tried like written here  
[https://stackoverflow.com/questions/42312319/how-do-i-add-a-dimension-to-an-array-opposite-of-squeeze](https://stackoverflow.com/questions/42312319/how-do-i-add-a-dimension-to-an-array-opposite-of-squeeze)

and here:  
[https://stackoverflow.com/questions/58217442/adding-an-additional-dimension-to-an-array](https://stackoverflow.com/questions/58217442/adding-an-additional-dimension-to-an-array)

but the result everytime is:

```julia
25701×1 Array{Any,2}:
[0.36919015509523057 0.3868927477017365 … 0.0 0.0; 0.36526140799820705 0.37552196118488257 … 0.0 0.0;

```

which is not what i need

i need it for a conv2d using keras  
im pretty sure in python is like this: `x.reshape(x.shape + (1,))`

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [December 8, 2020, 12:05am UTC](https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431/2 "2020-12-08T00:05:33Z")

</div>

Sounds like you’re not trying to add a dimension at all, but instead convert a 1D array of 2D arrays into a 4D array, with the first dimension corresponding to the outer array, and the last dimension being 1. The following isn’t pretty and it isn’t general, but it will do exactly that:

```julia
function toconvkernel(A::AbstractArray{<:AbstractMatrix})
    s = size(first(A))
    @assert all(==(s), size.(A))

    B = Array{eltype(eltype(A)), 4}(undef, length(A), s..., 1)
    for (i, a) in enumerate(A)
        B[i, :, :, 1] = a
    end
    B
end

```

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [December 8, 2020, 12:36am UTC](https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431/3 "2020-12-08T00:36:09Z")

</div>

For something that looks a little closer to the Python implementation, you might try

```julia
toconvkernel(A) = mapreduce(x->reshape(x, 1, size(x)..., 1), vcat, A)

```

Use `reshape` to convert each element of the outer array to a 4-dimensional array, then concatenate them along the first axis.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [December 8, 2020, 4:10pm UTC](https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431/4 "2020-12-08T16:10:06Z")

</div>

It did not occur to me before, but probably the best way is with Tullio

```julia
using Tullio
@tullio B[i, j, k, 1] := $A[i][j, k]

```

@contradict Note that the `mapreduce` way unfortunately scales very poorly due to repeated allocation (19s vs 14ms for a 1000x100x40x1 example)

---

<div class="post-metadata">

**Author:** ![lorrp1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lorrp1/32/17647_2.png) [@lorrp1](https://discourse.julialang.org/u/lorrp1)\
**Post date:** [December 8, 2020, 5:38pm UTC](https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431/5 "2020-12-08T17:38:12Z")

</div>

@tomerarnon

hello, thank you but its not working  
i tried with:  
**LoadError: “can’t use index 25701 on LHS for a new array”**  
`@tullio B[25701, 100, 40, 1] := $x[25701][100, 40]`

but it would be way better if it is a faster method compared to the other 2

the first returns:  
`MethodError: no method matching toconvkernel(::Array{Any,1})`

@contradict  
even though it is slow and occupy a lot of memory it is working, thanks:  
result: `25701×100×40×1 Array{Float64,4}:`

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 8, 2020, 5:45pm UTC](https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431/6 "2020-12-08T17:45:23Z")

</div>

There is a package with functions doing exactly this: [https://github.com/JuliaData/SplitApplyCombine.jl](https://github.com/JuliaData/SplitApplyCombine.jl). Specifically, you need `combinedims` or `combinedimsview` function from there.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 8, 2020, 6:22pm UTC](https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431/7 "2020-12-08T18:22:25Z")

</div>

The index letters in these einsum-like expressions are loop variables, not sizes. It should run literally as written (although without the dollar sign) if `A` is your vector of matrices:

> [@tomerarnon](#):
>
> `@tullio B[i, j, k, 1] := A[i][j, k]`

Ordering things `@tullio B[j, k, 1, i] := A[i][j, k]` should be faster – the last index of Julia’s arrays is the biggest step in memory.

There are also many ways to make array-like views of all the slices, LazyStack.jl or JuliennedArrays.jl are two small packages for exactly this, and SplitApplyCombine.jl as mentioned seems to also include this.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [December 8, 2020, 8:01pm UTC](https://discourse.julialang.org/t/problem-adding-a-dimesion-to-array-vector/51431/8 "2020-12-08T20:01:15Z")

</div>

> [@lorrp1](#):
>
> the first returns:  
> `MethodError: no method matching toconvkernel(::Array{Any,1})`

I defined `toconvkernel` as taking in an array with element type `<:AbstractMatrix` and you seem to have `Array{Any, 1}` (the eltype is `Any`) instead. You can remove the type annotation from the function definition, but it would be better to have your array not be an array of `Any` in the first place. Wherever you create that, it would be better to make sure it is strictly typed.
