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

**URL:** <https://discourse.julialang.org/t/removing-the-first-index-along-n-1-dimensions-of-an-array/86246>\
**Category:** General Usage\
**Tags:** question, indexing, arrays\
**Created:** [August 24, 2022, 8:07am UTC](https://discourse.julialang.org/t/removing-the-first-index-along-n-1-dimensions-of-an-array/86246 "2022-08-24T08:07:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luis\_Calderon](https://avatars.discourse-cdn.com/v4/letter/l/5e9695/32.png) [@Luis\_Calderon](https://discourse.julialang.org/u/Luis_Calderon)\
**Post date:** [August 24, 2022, 8:07am UTC](https://discourse.julialang.org/t/removing-the-first-index-along-n-1-dimensions-of-an-array/86246/1 "2022-08-24T08:07:12Z")

</div>

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:

```julia
    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)
    end

```

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 🙂

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 24, 2022, 9:38am UTC](https://discourse.julialang.org/t/removing-the-first-index-along-n-1-dimensions-of-an-array/86246/2 "2022-08-24T09:38:07Z")

</div>

> [@Luis\_Calderon](#):
>
> I need an implementation for N \> 2,

can you explain the need in more detail?

Does this come close to what you need?

```julia
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)

```julia

f(c)=sum((==(1)).(c.I))!=length(c.I)-1

```

---

<div class="post-metadata">

**Author:** ![Luis\_Calderon](https://avatars.discourse-cdn.com/v4/letter/l/5e9695/32.png) [@Luis\_Calderon](https://discourse.julialang.org/u/Luis_Calderon)\
**Post date:** [August 24, 2022, 11:20am UTC](https://discourse.julialang.org/t/removing-the-first-index-along-n-1-dimensions-of-an-array/86246/3 "2022-08-24T11:20:44Z")

</div>

Thank you for replying! This is fantastic. 🙂 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 🙂
