Hello,
I've recently come across a situation where I needed to index an array
a[:,:,i,j]
without having to know how many colons I needed.
I accomplished this with the following code.
function recursive_slicedim(A,dims,inds)
d_len = length(dims)
s = slicedim(A,dims[d_len],inds[d_len])
if d_len != 1
return recursive_slicedim(s,dims[1:d_len-1],inds[1:d_len-1])
else
return s
end
end
function tail_index(A,inds)
s_len = length(size(A))
i_len = length(inds)
dims = tuple([s_len - i for i=i_len-1:-1:0]...)
return squeeze(recursive_slicedim(A,dims,inds),dims)
end
a = rand(2,2,2,2)
tail_index(a,(1,2)) == a[:,:,1,2]
b = rand(2,2,2)
tail_index(b,(1,2)) == b[:,1,2]
Does something like this already exist in Base?
How would I go about writing some syntactic sugar for this function?
Something like
a[*,1,2] == last_index(a,(1,2))