Find indices or corresponding values except given indices

Hi,
I’d like to know how to access the rest except for given indices.
For example,

A = [1 2; 3 4]  # 2 x 2
indices = findall(a -> a == 1, A)  # indices = [CartesianIndex(1, 1)]
# _indices = [CartesianIndex(2, 1), CartesianIndex(1, 2), CartesianIndex(2, 2)]

Of course, not using findall(a -> a != 1, A). Instead, a function should look like except(indices, A) = _indices.

In the 1-d case, this post makes sense, for example,

julia> A = [1, 2, 3, 4]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> A = -A
4-element Vector{Int64}:
 -1
 -2
 -3
 -4

julia> indices = findall(a -> a == -1, A)
1-element Vector{Int64}:
 1

julia> A[1:end .!= indices]
3-element Vector{Int64}:
 -2
 -3
 -4

Actually, my demand is for 1-d array so my issue is resolved.
BTW, any ideas for n-d arrays?

One way is to work with logical indices, i.e. boolean flags:

A = [1 2; 3 4]
flags = A .== 1
A[.!flags]

Another way is using Not from the InvertedIndices package:

using InvertedIndices
A = [1 2; 3 4]
indices = findall(a -> a == 1, A)
A[Not(indices)]
3 Likes