Concatenate ArrayViews (or sliceviews or SubArrays) in Julia?

Is there a way to combine ArrayViews in Julia that doesn't copy the underlying data? (I'd also love to use SubArray if that solves the problem.)

In the data referred to in the code below, for example, I want to make one array ArrayView y1

, and on y2

.

julia> x = [1:50];

julia> using ArrayViews;

julia> y1 = view(x, 2:5);

julia> y2 = view(x, 44:48);

julia> concat(y1, y2)  # I wish there were a function like this
ERROR: concat not defined

julia> [y1, y2]  # This copies the data in y1 and y2, unfortunately
9-element Array{Int64,1}:
  2
  3
  4
  5
 44
 45
 46
 47
 48

      

+3


source to share


1 answer


Not directly. But you can roll your type with something like:

julia> type CView{A<:AbstractArray} <: AbstractArray
       a::A
       b::A
       end

julia> import Base: size, getindex, setindex!

julia> size(c::CView) = tuple([sa+sb for (sa, sb) in zip(size(c.a), size(c.b))]...)
size (generic function with 57 methods)

julia> getindex(c::CView, i::Int) = i <= length(c.a) ?  getindex(c.a, i) : getindex(c.b, i)
getindex (generic function with 180 methods)

julia> c = CView(y1, y2);

julia> size(c)
(9,)

julia> c[1]
2

julia> c[4]
5

julia> c[5]
48

      



These methods may not be optimal, but they can certainly get you started. More methods may be needed to be useful. Note that the key simply decides which member array to index. For multidimensional indexing, sub2ind

you can use.

+1


source







All Articles