Sleepy
1
Given is a vector of vectors
s = [[3, 2, 1, 1, 1],[3, 1, 2, 1, 3]]
where all vectors have the same length and an index vector
i = [3,4]
Is there an easy way to get the subvectors of s, starting at index i.e. 3 to end for the first vector and 4 to end for the second, and so on?
In the example above, the result should be
s_suff = [[1,1,1], [1,3]]
I tried
s_suff = [s[i][p:end] for i in 1:length(s)]
Thanks
lmiq
2
I think you were almost there (you needed p[i]
)
julia> s = [[3, 2, 1, 1, 1],[3, 1, 2, 1, 3]]
2-element Array{Array{Int64,1},1}:
[3, 2, 1, 1, 1]
[3, 1, 2, 1, 3]
julia> p = [3,4]
2-element Array{Int64,1}:
3
4
julia> [ s[i][p[i]:end] for i in 1:length(s) ]
2-element Array{Array{Int64,1},1}:
[1, 1, 1]
[1, 3]
ps: use backticks to mark the code, makes reading much easier.
5 Likes
Perhaps a slightly shorter/more readable version would be:
julia> s = [[3, 2, 1, 1, 1],[3, 1, 2, 1, 3]];
julia> p = [3,4];
julia> [ S[P:end] for (S,P) in zip(s,p) ]
2-element Array{Array{Int64,1},1}:
[1, 1, 1]
[1, 3]
1 Like