# How to best reshape this array

**URL:** <https://discourse.julialang.org/t/how-to-best-reshape-this-array/15919>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 5, 2018, 9:21am UTC](https://discourse.julialang.org/t/how-to-best-reshape-this-array/15919 "2018-10-05T09:21:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [October 5, 2018, 9:21am UTC](https://discourse.julialang.org/t/how-to-best-reshape-this-array/15919/1 "2018-10-05T09:21:59Z")

</div>

I have a vector of matrices:

```julia
julia> n = 4
4

julia> sz = (5,6)
(5, 6)

julia> imgs = [i*ones(Int, sz...) for i in 1:n]
4-element Array{Array{Int64,2},1}:
 [1 1 … 1 1; 1 1 … 1 1; … ; 1 1 … 1 1; 1 1 … 1 1]
 [2 2 … 2 2; 2 2 … 2 2; … ; 2 2 … 2 2; 2 2 … 2 2]
 [3 3 … 3 3; 3 3 … 3 3; … ; 3 3 … 3 3; 3 3 … 3 3]
 [4 4 … 4 4; 4 4 … 4 4; … ; 4 4 … 4 4; 4 4 … 4 4]

```

I want to keep this form, but I also need another form: a matrix where each cell is a vector containing all the equivalent elements of the matrices from before:

```julia
julia> noise = [[imgs[j][i] for j in 1:n] for i in CartesianIndices(sz)]
5×6 Array{Array{Int64,1},2}:
 [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
 [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
 [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
 [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
 [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]

julia> noise[2,3] # for example
4-element Array{Int64,1}:
 1
 2
 3
 4

```

Because I won’t need every single one of those vector-of-elements I’m using [MappedArrays](https://github.com/JuliaArrays/MappedArrays.jl) on the `noise` matrix to save time. I’m looking however into using some `@view` wizardry or some such for the reshaping step (turning the vector of matrices into a matrix of vectors)… Anyone knows a cool trick I can use?

For context: this is a vector of images (1440x1080 pixels) and can be 5000 images long. I’m accessing ~5000 pixels in this 10^10 soup.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 5, 2018, 2:31pm UTC](https://discourse.julialang.org/t/how-to-best-reshape-this-array/15919/2 "2018-10-05T14:31:59Z")

</div>

You can first lay out the data then view into it in 2 different ways. Based on which access pattern will be used more often, you can also permute the dims of the data array to make it more cache-friendly.

```julia
julia> n = 4;

julia> sz = (5, 6);

julia> imgs = [i for I in CartesianIndices(sz), i in 1:n];

julia> imgs[:,:,1]
5×6 Array{Int64,2}:
 1 1 1 1 1 1
 1 1 1 1 1 1
 1 1 1 1 1 1
 1 1 1 1 1 1
 1 1 1 1 1 1

julia> imgs[:,:,2]
5×6 Array{Int64,2}:
 2 2 2 2 2 2
 2 2 2 2 2 2
 2 2 2 2 2 2
 2 2 2 2 2 2
 2 2 2 2 2 2

julia> getimgview(imgs, i) = @view imgs[:,:,i]
getimgview (generic function with 1 method)

julia> getpixelview(imgs, i, j) = @view imgs[i,j,:]
getpixelview (generic function with 1 method)

julia> getimgview(imgs, 1)
5×6 view(::Array{Int64,3}, :, :, 1) with eltype Int64:
 1 1 1 1 1 1
 1 1 1 1 1 1
 1 1 1 1 1 1
 1 1 1 1 1 1
 1 1 1 1 1 1

julia> getimgview(imgs, 2)
5×6 view(::Array{Int64,3}, :, :, 2) with eltype Int64:
 2 2 2 2 2 2
 2 2 2 2 2 2
 2 2 2 2 2 2
 2 2 2 2 2 2
 2 2 2 2 2 2

julia> getpixelview(imgs, 1, 2)
4-element view(::Array{Int64,3}, 1, 2, :) with eltype Int64:
 1
 2
 3
 4

```

More elegantly, you can probably define a wrapper around `imgs` and overload `getindex` for 1 and 2 indices.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [October 8, 2018, 2:59pm UTC](https://discourse.julialang.org/t/how-to-best-reshape-this-array/15919/3 "2018-10-08T14:59:11Z")

</div>

This worked nicely, thanks!
