# Stack multidimensional Array

**URL:** <https://discourse.julialang.org/t/stack-multidimensional-array/4632>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 4, 2017, 5:12am UTC](https://discourse.julialang.org/t/stack-multidimensional-array/4632 "2017-07-04T05:12:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saran\_S](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saran_s/32/1155_2.png) [@Saran\_S](https://discourse.julialang.org/u/Saran_S)\
**Post date:** [July 4, 2017, 5:12am UTC](https://discourse.julialang.org/t/stack-multidimensional-array/4632/1 "2017-07-04T05:12:40Z")

</div>

Hello,

I would like to know if there is better way to stack multidimensional array like `vcat` Below is the code which i am working with.

```julia
data1 = rand(10000,3072);
data2 = rand(10000,3072);
data3 = rand(10000,3072);
data4 = rand(10000,3072);
data5 = rand(10000,3072);

function stack_Array_1()
  Xaxis = [];
  for i=1:5
    push!(Xaxis,eval(Symbol("data$i")))
  end
  Xaxis
end

function stack_Array_2()
  Yaxis = Array{Float64,2}(0,3072)
  for i=1:5
    Yaxis = vcat(Yaxis,eval(Symbol("data$i")))
  end
  Yaxis
end

```

```julia
@btime stack_Array_1();
  2.319 μs (33 allocations: 1.61 KiB)

Any[5]
10000×3072 Array{Float64,2}:
10000×3072 Array{Float64,2}:
10000×3072 Array{Float64,2}:
10000×3072 Array{Float64,2}:
10000×3072 Array{Float64,2}:

```

```julia
@btime stack_Array_2();
   1.973 s (75 allocations: 3.43 GiB)

50000×3072 Array{Float64,2}:
 0.635014 0.462685 0.185559 0.295888 … 0.123956 0.971991 0.559269
 0.409272 0.57993 0.820811 0.993251 0.12653 0.553527 0.577177
 0.660994 0.114057 0.702278 0.119705 0.354153 0.681063 0.057957
 0.11004 0.729124 0.25563 0.717678 … 0.554839 0.800087 0.779025
 0.854646 0.217248 0.834483 0.49127 0.245325 0.748648 0.725246
 0.733918 0.799065 0.349517 0.917985 0.619041 0.0812406 0.321144
 ⋮ ⋱ ⋮                  
 0.397121 0.578101 0.832732 0.508987 0.85815 0.61081 0.735447
 0.355548 0.771022 0.584872 0.0710232 0.901111 0.567234 0.735604
 0.281855 0.117889 0.164787 0.719332 0.359149 0.668798 0.570658
 0.0783613 0.947521 0.327537 0.722403 … 0.152016 0.173811 0.346503
 0.905858 0.611356 0.158429 0.0897009 0.788216 0.790752 0.968152
 0.293524 0.558019 0.123042 0.221605 0.325241 0.666398 0.310829

```

I would like the Arrays to be vertically concatenated which is done by function `stack_Array_2()` but the performance is poor. `stack_Array_1() ` does perform good but iam not sure if there is a way to stack the arrays. Kindly let me know if there is any way to get the desired result.

Thank You.

---

<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:** [July 4, 2017, 7:24am UTC](https://discourse.julialang.org/t/stack-multidimensional-array/4632/2 "2017-07-04T07:24:14Z")

</div>

```julia
dataarrays = [rand(10000, 3072) for _ in 1:5]

vcat(dataarrays...)

```

Don’t use `eval` here. Don’t `vcat` recursively, use the `...` operator.

---

<div class="post-metadata">

**Author:** ![Saran\_S](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saran_s/32/1155_2.png) [@Saran\_S](https://discourse.julialang.org/u/Saran_S)\
**Post date:** [July 4, 2017, 8:54am UTC](https://discourse.julialang.org/t/stack-multidimensional-array/4632/3 "2017-07-04T08:54:35Z")

</div>

@Tamas_Papp

> dataarrays = [rand(10000, 3072) for \_ in 1:5]

I am working on [CIFAR10](http://www.cs.toronto.edu/~kriz/cifar.html) python pickle dataset. Which has 5 batch (type Array{UInt8,2}(10000,3072) each) files with 10000 records in each. Hence had use 5 different `rand` arrays as an example. But Thank you for the above list comprehension method it will be useful for testing.

> Don’t use eval here

Kindly elaborate. since i had to loop over 5 different datasets, i thought that was my option to loop over all of it. I will be glad to learn if thats the wrong way to go about it.

> use the … operator.

Thank you for the splat operator. Performance has improved a bit for the below code Please let me know if i am doing anything wrong.

```julia
@btime final_data1 = stack_Array_1();
  2.353 μs (33 allocations: 1.61 KiB)

```

```julia
@btime final_data2 = stack_Array_2();
  2.274 s (75 allocations: 3.43 GiB)

```

```julia
@btime final_data3 = stack_Array_3();
  668.435 ms (49 allocations: 1.14 GiB)

```

```julia
function stack_Array_1()
  Xaxis = [];
  for i=1:5
    push!(Xaxis,eval(Symbol("data$i")))
  end
  Xaxis
end

function stack_Array_2()
  Yaxis = Array{Float64,2}(0,3072)
  for i=1:5
    Yaxis = vcat(Yaxis,eval(Symbol("data$i")))
  end
  Yaxis
end

function stack_Array_3()
  Xaxis = [];
  for i=1:5
    push!(Xaxis,eval(Symbol("data$i")))
  end
  vcat(Xaxis...)
end

```

---

<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:** [July 4, 2017, 9:02am UTC](https://discourse.julialang.org/t/stack-multidimensional-array/4632/4 "2017-07-04T09:02:10Z")

</div>

> [@Saran\_S](#):
>
> Please let me know if i am doing anything wrong.

I told you above, yet you are posting the same code (with `eval` and recursive `vcat`).

`eval` is not necessary here. Generally, you should not touch `eval` unless for generated code.

I understand that `rand` is for the MWE, but just read whatever data structure you have into a vector of arrays, if that’s the most convenient.

---

<div class="post-metadata">

**Author:** ![Saran\_S](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saran_s/32/1155_2.png) [@Saran\_S](https://discourse.julialang.org/u/Saran_S)\
**Post date:** [July 4, 2017, 9:09am UTC](https://discourse.julialang.org/t/stack-multidimensional-array/4632/5 "2017-07-04T09:09:23Z")

</div>

@Tamas_Papp

> you should not touch eval unless for generated code.

Understood. I will keep this in mind. I didn’t know any other way to iterate to **variables** in for loop hence i had to use `eval`

Below is my code which i am working on. And trying to fix the performance issue with Stacking the arrays.

```julia
using PyCall
@pyimport pickle

function load_pickle_data(ROOT)
	xs=[]
	ys=[]

 	for b=1:5
		f=joinpath(ROOT, "data_batch_$b")
		X,Y = pickle_batch(f)

		push!(xs,X)
		push!(ys,Y)

	end
	(vcat(xs...),ys)
end

function pickle_batch(file)
	fo=open(file,"r")
	datadict = pickle.loads(pybytes(read(fo)))
	X=datadict["data"]
	Y=datadict["labels"]
	(X,Y)
end

```

---

<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:** [July 4, 2017, 12:36pm UTC](https://discourse.julialang.org/t/stack-multidimensional-array/4632/6 "2017-07-04T12:36:30Z")

</div>

> [@Saran\_S](#):
>
> I didn’t know any other way to iterate to variables in for loop hence i had to use eval

Don’t iteratively create variables in a loop in normal code. Use some other data structure (an array, a dictionary, etcetera).
