Exclude elements of Array based on index (Julia) Exclude elements of Array based on index (Julia) arrays arrays

Exclude elements of Array based on index (Julia)


Not as terse as the R equivalent, but fairly readable:

A[1:end .!= k]

More importantly, this can be used in multidimensional arrays too, e.g.

B[  1:end .!= i,   1:end .!= j,   1:end .!= k  ]


Have a look at deleteat!. Examples:

julia> A = [1,2,3,4,5,6,7,8]; k = 4;julia> deleteat!(A, k)7-element Array{Int64,1}: 1 2 3 5 6 7 8julia> A = [1,2,3,4,5,6,7,8]; k = 2:2:8;julia> deleteat!(A, k)4-element Array{Int64,1}: 1 3 5 7


The simplest way is to use "Not".

julia> using InvertedIndices # use ] add InvertedIndices if not installedjulia> A = [1,2,3,4,5,6,7,8]julia> k = 4;julia> A[Not(k)]7-element Array{Int64,1}: 1 2 3 5 6 7 8