Recently I use tensor so many times so I implement head
(same to front
in this thread) and tail
by meta programming. I wish one could save time using my functions.
head(x) = eval(Meta.parse("$(x)[$(":, " ^ (length(size(x))-1)) 1:(end-1)]"))
tail(x) = eval(Meta.parse("$(x)[$(":, " ^ (length(size(x))-1)) 2: end ]"))
head
and tail
detect last dimension of array and drop a last or first slice in the dimenson.
Example
vector:
julia> @show X = rand(0:9, 10);
X = rand(0:9, 10) = [3, 9, 0, 2, 8, 6, 8, 9, 2, 9]
julia> @show head(X);
head(X) = [3, 9, 0, 2, 8, 6, 8, 9, 2]
julia> @show tail(X);
tail(X) = [9, 0, 2, 8, 6, 8, 9, 2, 9]
matrix:
julia> X_ = rand(0:9, 3, 3)
3×3 Matrix{Int64}:
0 8 7
0 5 8
6 4 1
julia> head(X_)
3×2 Matrix{Int64}:
0 8
0 5
6 4
julia> tail(X_)
3×2 Matrix{Int64}:
8 7
5 8
4 1
3-tensor:
julia> X__ = rand(0:1, 3, 3, 3)
3×3×3 Array{Int64, 3}:
[:, :, 1] =
1 0 0
1 1 1
1 0 1
[:, :, 2] =
1 0 1
1 1 0
1 0 1
[:, :, 3] =
0 0 0
0 1 0
0 1 1
julia> head(X__)
3×3×2 Array{Int64, 3}:
[:, :, 1] =
1 0 0
1 1 1
1 0 1
[:, :, 2] =
1 0 1
1 1 0
1 0 1
julia> tail(X__)
3×3×2 Array{Int64, 3}:
[:, :, 1] =
1 0 1
1 1 0
1 0 1
[:, :, 2] =
0 0 0
0 1 0
0 1 1
vector of vector:
julia> head([[2, 3], [4, 6, 7], ["s"]])
2-element Vector{Vector}:
[2, 3]
[4, 6, 7]
julia> tail([[2, 3], [4, 6, 7], ["s"]])
2-element Vector{Vector}:
[4, 6, 7]
["s"]