# How to intersect a Cartesian Index with a regular index?

**URL:** <https://discourse.julialang.org/t/how-to-intersect-a-cartesian-index-with-a-regular-index/70270>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 23, 2021, 8:15pm UTC](https://discourse.julialang.org/t/how-to-intersect-a-cartesian-index-with-a-regular-index/70270 "2021-10-23T20:15:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 23, 2021, 8:15pm UTC](https://discourse.julialang.org/t/how-to-intersect-a-cartesian-index-with-a-regular-index/70270/1 "2021-10-23T20:15:09Z")

</div>

For example, I have an Index1 that is generated from this:

```julia
A = [NaN, 2.1, NaN, -3, 8, 65, NaN];
f1(A) = [idx for idx in CartesianIndices(A) if @inbounds (isnan(A[idx])) ];
Index1 = f1(A);

```

Results of Index1:

```julia
Index1 = 3-element Vector{CartesianIndex{1}}:
 CartesianIndex(1,)
 CartesianIndex(3,)
 CartesianIndex(7,)

```

An Index2 generated from:

```julia
C = [1 2 3 4 5 6 7]
Index2 = C.>1

```

So my Index2 is

```julia
1×7 BitMatrix:
 0 1 1 1 1 1 1

```

How do I get their intersect, i.e, the Index of 3 and 7 as a Boolean array of [0, 0,1,0,0,0,1]?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [October 23, 2021, 9:01pm UTC](https://discourse.julialang.org/t/how-to-intersect-a-cartesian-index-with-a-regular-index/70270/2 "2021-10-23T21:01:44Z")

</div>

Using [InvertedIndices.jl](https://github.com/JuliaData/InvertedIndices.jl), you could do this with something like:

```julia
Ind2[Not(Index1)] .= false

```

where `Ind2` is now the intersection of both.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 23, 2021, 9:12pm UTC](https://discourse.julialang.org/t/how-to-intersect-a-cartesian-index-with-a-regular-index/70270/3 "2021-10-23T21:12:07Z")

</div>

You could also keep the coherency in your code and define in a similar way:

```julia
C = [1, 2, 3, 4, 5, 6, 7]
Index2 = [idx for idx in CartesianIndices(C) if @inbounds (C[idx]>1) ]
intersect(Index1, Index2)

2-element Vector{CartesianIndex{1}}:
 CartesianIndex(3,)
 CartesianIndex(7,)

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 23, 2021, 9:22pm UTC](https://discourse.julialang.org/t/how-to-intersect-a-cartesian-index-with-a-regular-index/70270/4 "2021-10-23T21:22:38Z")

</div>

Many thanks for the replies!

For my data processing, I have to do the intersect based on the new Index2 and the inverse of Index1. Unfortunately, even if I generate my Index2 using the same approach, the below code gives me an error:

```julia
using InvertedIndices
intersect(Not(Index1), Index2)

```

Error message:  
`MethodError: no method matching length(::InvertedIndex{Vector{CartesianIndex{1}}})`

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 23, 2021, 9:27pm UTC](https://discourse.julialang.org/t/how-to-intersect-a-cartesian-index-with-a-regular-index/70270/6 "2021-10-23T21:27:57Z")

</div>

I do need my Index1. It was generated through a very complex process. Basically, I need to first process the first batch of data based on Index1, then I need to find matching data from the remaining rows, thus the need for an inverse Index1.

It seems that the Cartesian index is not very user friendly after all. Maybe I should try to figure out how to generate a regular Boolean index in the first place?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 23, 2021, 9:30pm UTC](https://discourse.julialang.org/t/how-to-intersect-a-cartesian-index-with-a-regular-index/70270/7 "2021-10-23T21:30:23Z")

</div>

Could you use:

```julia
ix = CartesianIndices(A).∉ Ref(Index1)

7-element BitVector:
 1
 0
 1
 0
 0
 0
 1

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 23, 2021, 9:31pm UTC](https://discourse.julialang.org/t/how-to-intersect-a-cartesian-index-with-a-regular-index/70270/8 "2021-10-23T21:31:36Z")

</div>

That works! Thank you so much.
