ArrayViews.jl icon indicating copy to clipboard operation
ArrayViews.jl copied to clipboard

getindex with UnitRange

Open samuela opened this issue 11 years ago • 11 comments

x = reshape(1:25, 5, 5)
v = view(x, 1:2:5, 1:2:5)
v[:,1]

produces

ERROR: `getindex` has no method matching getindex(::StridedView{Int64,2,0,Array{Int64,2}}, ::UnitRange{Int64}, ::Int64)

Selecting by range is very common and I believe should be supported. Is there a reason why it is not?

samuela avatar Dec 30 '14 05:12 samuela

I can reproduce this.

I can't comment on ArrayViews, but this works if you use sub instead of view.

timholy avatar Dec 30 '14 05:12 timholy

Unfortunately, I need to use strided_view so replacing it with sub would be tricky.

samuela avatar Dec 30 '14 05:12 samuela

Out of curiosity, why do you need to use strided_view? I ask because I believe it's fair to say that in 0.4, sub (or slice) will be the recommended way to create views. If there's code out there that requires StridedView inputs, it might be time to start thinking about loosening that typing.

timholy avatar Dec 30 '14 05:12 timholy

I'm trying to create a moving window view. For example, I have a function

function moving_window(arr, n)
    shp = (size(arr, 1) * n, size(arr, 2) - n + 1)
    strided_view(arr, shp, ArrayViews.contrank(arr), strides(arr))
end

In my case, n is ~30 and I have a ~1gb dataset. I can handle ~1gb but not as easily ~30gb.

samuela avatar Dec 30 '14 05:12 samuela

Does sub(arr, :, size(arr, 2)-n+1:size(arr,2)) not work?

timholy avatar Dec 30 '14 12:12 timholy

No, sub only construct subarray views as far as I know.

julia> x = reshape(1:12, 2, 6)
2x6 Array{Int64,2}:
 1  3  5  7   9  11
 2  4  6  8  10  12

julia> moving_window(x, 2)
4x5 StridedView{Int64,2,2,Array{Int64,2}}:
 1  3  5   7   9
 2  4  6   8  10
 3  5  7   9  11
 4  6  8  10  12

julia> sub(x, :, size(x, 2)-2+1:size(x, 2))
2x2 SubArray{Int64,2,Array{Int64,2},(UnitRange{Int64},UnitRange{Int64})}:
  9  11
 10  12

samuela avatar Dec 30 '14 18:12 samuela

This does this trick for me:

getindex(av::ArrayView, I::Union(Int,UnitRange{Int})...) = sub(av,I...)

samuela avatar Dec 30 '14 21:12 samuela

Now I understand what you're after---thanks for the demo. I agree, currently there isn't a great way to achieve that without directly messing with strides.

timholy avatar Dec 31 '14 01:12 timholy

Hi, @samuela, can you make a PR to add your trick to the code?

lindahua avatar Mar 09 '15 07:03 lindahua

I also have this problem with Julia 0.3, but not 0.4

galenlynch avatar Aug 03 '15 02:08 galenlynch

Is this still relevant to track?

andreasnoack avatar Jun 05 '18 08:06 andreasnoack