Removing the first index along N-1 dimensions of an Array

Dear Julia Community,

This is my first time posting, so I hope I meet the standards put in place. Thank you for all of this btw!!

Problem: Let me illustrate with an example. For a 3D array, I’d like to remove any component (i,j,k) where two of the indices equal 1 e.g., i=1, j=1, k=5 or i=3, j=1, k=1 and create a vector of the remaining elements. I’d also like to store the removed elements and the respective indices. I need an implementation for N > 2, so more general. I can’t say I have a MWE, but I started to write this:

    grid            = 10
    A               = zeros(grid, grid, grid) 
    dimension       = ndims(A)
    colons_upper    = ()
    colons_lower    = ntuple(_ -> (:), dimension)
    condition       = 1:grid .!= 1

    for d in axes(colons_lower)
        A = A[colons_upper..., condition, colons_lower...]
        colons_upper = tuple(colons_upper..., :)
        colons_lower = ntuple(_ -> (:), dimension - d)

Issue is, this removes the first row of every slice, first column of every slice thereafter, and then, the first slice … Thank you in advance and please let me know if I didn’t meet the posting standards :slightly_smiling_face:

can you explain the need in more detail?

Does this come close to what you need?

m=rand(1:100, 4,3,5)

f(c)=!(c.I[1]==1 &&  c.I[2]==1) &&
    !(c.I[2]==1 &&  c.I[3]==1) &&
    !(c.I[1]==1 &&  c.I[3]==1)

m[filter(f, CartesianIndices(size(m)))]

more generally, the following predicate could be used (it remains to clarify / specify the case i = j = k = … = 1)


Thank you for replying! This is fantastic. :slight_smile: and sorry for not being clear there! I should have said when the Array is of 3 or more dimensions (where N referred to dimension of the array).

Regarding the special case of i=j=k=…=1, I actually need to retain that, so I should have said where at least N-1 of the indices equal 1. Since your implementation does just that, I’ll mark it as the solution :slight_smile: