# \`view\` array indexes that vary as a function of another index of that subarray?

**URL:** <https://discourse.julialang.org/t/view-array-indexes-that-vary-as-a-function-of-another-index-of-that-subarray/17531>\
**Category:** General Usage\
**Created:** [November 14, 2018, 9:24pm UTC](https://discourse.julialang.org/t/view-array-indexes-that-vary-as-a-function-of-another-index-of-that-subarray/17531 "2018-11-14T21:24:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [November 14, 2018, 9:24pm UTC](https://discourse.julialang.org/t/view-array-indexes-that-vary-as-a-function-of-another-index-of-that-subarray/17531/1 "2018-11-14T21:24:38Z")

</div>

In a `view` is there a way to define an index of an array that varies as a function of another index of that array?  
i.e. the indexing of dim 1 varies along dim 2.

I’d love to avoid making a memory copy of the subarray. My application goal is to define a view to a 2D region of a video that changes position in time (the 3rd dimension), and read the full 3 dimensional output without creating a memory copy of the subarray.

An illustrative simplistic part-pseudocode example, where the resulting assignment of 1’s in `data` follows a sinusoidal shape:

```julia
f(x) = round.(Int,5 .+ (sin.((x)/2)*5))

data = zeros(Int,11,10)

dim2 = : #pseudocode
subarray = view(data,f(dim2),dim2) #pseudocode

subarray .= 1

data
--
11×10 Array{Int64,2}:
 0 0 0 0 0 0 0 0 1 1
 0 0 0 0 0 0 0 1 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 1 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 0 0 0 0
 1 0 0 0 0 0 0 0 0 0
 0 0 0 0 1 0 0 0 0 0
 0 1 0 0 0 0 0 0 0 0
 0 0 1 1 0 0 0 0 0 0

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 14, 2018, 9:56pm UTC](https://discourse.julialang.org/t/view-array-indexes-that-vary-as-a-function-of-another-index-of-that-subarray/17531/2 "2018-11-14T21:56:32Z")

</div>

Perhaps something like:

```julia
# Maps `getindex` by `f` and then indexes into `A`
struct MappedArray{F, T, N, A <: AbstractArray} <: AbstractArray{T, N}
    f::F
    a::A
end
MappedArray(f::F, A::AbstractArray{T, N}) where {F, T, N} = MappedArray{F, T, N, typeof(A)}(f, A)
Base.size(A::MappedArray) = size(A.a)
Base.getindex(A::MappedArray, i::Integer) = A.a[A.f(i)]

a = 1:10
z = length(a)
m = MappedArray(x -> rand(1:z), a)

```

Where we have an array that randomly picks an index for `a`:

```julia
julia> m[1]
9

julia> m[1]
1

julia> m[1]
8

```

Now we can create a view with it:

```julia
julia> v = rand(100);

julia> c = view(v, m)
10-element view(::Array{Float64,1}, [4, 6, 9, 5, 10, 3, 9, 10, 10, 8]) with eltype Float64:
 0.8159868211487065
 0.5112093449671173
 0.4704287700375531
 0.6906170863362935
 0.6906170863362935
 0.5112093449671173
 0.5112093449671173
 0.15705852167037837
 0.4704287700375531
 0.4589219238937361

julia> c
10-element view(::Array{Float64,1}, [4, 2, 1, 7, 1, 4, 9, 8, 7, 7]) with eltype Float64:
 0.4589219238937361
 0.6779349956175409
 0.8159868211487065
 0.8159868211487065
 0.6906170863362935
 0.15705852167037837
 0.8159868211487065
 0.48428708295030143
 0.4704287700375531
 0.48428708295030143

```

so in theory you should be able to just come up with your `f`.

---

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [November 14, 2018, 10:19pm UTC](https://discourse.julialang.org/t/view-array-indexes-that-vary-as-a-function-of-another-index-of-that-subarray/17531/3 "2018-11-14T22:19:17Z")

</div>

Wow, that’s a really nice approach!  
I’m trying to figure out how to make it handle multidimensional source arrays & views.

---

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [November 15, 2018, 3:11am UTC](https://discourse.julialang.org/t/view-array-indexes-that-vary-as-a-function-of-another-index-of-that-subarray/17531/4 "2018-11-15T03:11:13Z")

</div>

The more I think about the nature of indexing required for this, I’m realising that for my particular application of subregion video registration without creating memory copies, the below may actually be an acceptable approach.

I’ll need to explore the overhead of interacting with the array of 2D views vs. a 3D array, but it allows the 2D subregions to be shifted for registration.

One question though is what the best way to preallocate the `subdata_arrayofviews` object as I’m only defining the number of array `SubArray` elements, without preallocating the `SubArray` itself. Perhaps thats insignificant though.

```julia
using BenchmarkTools

data = rand(UInt8,1000,1000,1000)

x_sub = 100:200
y_sub = 200:400

function method1(data,x_sub,y_sub)
    subdata_singleview = view(data,x_sub,y_sub,:) #Standard non-translating view method.
end

function method2(data,x_sub,y_sub)
    subdata_arrayofviews = Array{SubArray}(undef,1000)
    for i = 1:size(data,3)
        x_sub_jitter = x_sub .+ rand(-10:10)
        y_sub_jitter = y_sub .+ rand(-10:10)
        subdata_arrayofviews[i] = view(data,x_sub_jitter,y_sub_jitter,i)
    end
    return subdata_arrayofviews
end

@btime m1 = method1(data,x_sub,y_sub) #688.592 ns (6 allocations: 256 bytes)
@btime m2 = method2(data,x_sub,y_sub) #369.149 μs (7490 allocations: 281.20 KiB)

```

–  
I thought to try building an indexing vector based on collating the translated subregion indices for each 2D slice, then reshaping the view based on this discussion: [https://github.com/JuliaLang/julia/issues/4211](https://github.com/JuliaLang/julia/issues/4211)

However (and obviously… in hindsight) it seems that the indexing vector is sufficiently large to do away with any of the benefits of using a view, given each pixel index occupies an Int64.

If there was a way to represent this vector through a series of `UnitRange` perhaps it might save a decent amount of memory/allocations.

```julia
function method3(data,x_sub,y_sub)
    
    w = size(data,1)
    h = size(data,2)
    npix::UInt64 = w*h
    indexplane = reshape(1:npix,w,h)
    
    indexvec = Vector{Int64}(undef,0)
    for i = 1:size(data,3)
        x_sub_jitter = x_sub .+ rand(-10:10)
        y_sub_jitter = y_sub .+ rand(-10:10)
        append!(indexvec,vec(indexplane[x_sub_jitter,y_sub_jitter] .+ ((i-1)*npix)))
    end
    subdata_reshaped = reshape(view(data,indexvec),length(x_sub),length(y_sub),size(data,3))
end
@btime m3 = method3(data,x_sub,y_sub) #490.177 ms (6018 allocations: 350.78 MiB)

```
