# Index of a grid

**URL:** <https://discourse.julialang.org/t/index-of-a-grid/76771>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 19, 2022, 6:55pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771 "2022-02-19T18:55:05Z")\
**Posts on this page:** 9\
**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:** [February 19, 2022, 6:55pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771/1 "2022-02-19T18:55:05Z")

</div>

My goal is to find the index of a grid that meet certain criteria. For example, I have an X Y that is meshgridded together using the below function:  
`X, Y = ndgrid([-180:1:180], [-90:1:90];`

So my Y will be like a matrix like the below:

```julia
Y = [-90, -89, -88, ... 90;
...
-90, -89, -88, ... 90];

```

How do I find the Index of the grid with Y\>80, so that Y[Index] will give me a grid where Y\>80? Why didn’t the below work? It gives me a vector, instead of a matrix.  
`Ind = Y .> 80;`

Many thanks!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 19, 2022, 7:03pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771/2 "2022-02-19T19:03:11Z")

</div>

Can you make an MWE? I don’t think Y is a matrix because:

```julia
julia> Y = rand(5, 5);

julia> Y .> 0.5
5×5 BitMatrix:
 0 1 0 1 1
 0 1 1 0 0
 0 0 0 1 0
 0 1 1 1 0
 0 0 0 0 1

```

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [February 19, 2022, 7:03pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771/3 "2022-02-19T19:03:56Z")

</div>

Hi there, I tried the following and got a matrix:

```julia
julia> Y = rand(1:20, 5,5)
5×5 Matrix{Int64}:
  6 10 1 10 3
 16 11 12 3 11
 13 20 1 4 3
  1 9 1 12 1
 15 13 15 7 6
julia> Y .> 10
5×5 BitMatrix:
 0 0 0 0 0
 1 1 1 0 1
 1 1 0 0 0
 0 0 0 1 0
 1 1 1 0 0

```

However, if you want indices in another format, try:

```julia
julia> findall(Y .> 10)
10-element Vector{CartesianIndex{2}}:
 CartesianIndex(2, 1)
 CartesianIndex(3, 1)
 CartesianIndex(5, 1)
 CartesianIndex(2, 2)
 CartesianIndex(3, 2)
 CartesianIndex(5, 2)
 CartesianIndex(2, 3)
 CartesianIndex(5, 3)
 CartesianIndex(4, 4)
 CartesianIndex(2, 5)

```

---

<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:** [February 19, 2022, 7:28pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771/4 "2022-02-19T19:28:31Z")

</div>

Thanks for the reply.

It’s true that the Index is a matrix, but when you want to get the real values of the Y by doing the below:  
`Y[Y .> 0.5]`  
it will become a vector.

---

<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:** [February 19, 2022, 7:31pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771/5 "2022-02-19T19:31:30Z")

</div>

> [@Elmo](#):
>
> `findall(Y .> 10)`

Unfortunately, I got the same issue:

```julia
Ind = findall(Y .> 10);
Y_new = Y[Ind];

```

will give me a vector as well.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 19, 2022, 7:38pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771/6 "2022-02-19T19:38:30Z")

</div>

if I have this matrix:

```julia
julia> a = rand(0:1, 3,3)
3×3 Matrix{Int64}:
 1 0 0
 0 0 1
 1 0 0

```

what is the “grid” you expect/want to get by doing

```julia
a[a .> 0]

```

?

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [February 19, 2022, 7:49pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771/7 "2022-02-19T19:49:53Z")

</div>

Hmm do you want something like a matrix of only the selected terms and everything else zero? If so, then:

```julia
julia> Ynew = zeros(5,5)
5×5 Matrix{Float64}:
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0

julia> Ynew[inds] .= Y[inds];

julia> Ynew
5×5 Matrix{Float64}:
  0.0 0.0 0.0 0.0 0.0
 16.0 11.0 12.0 0.0 11.0
 13.0 20.0 0.0 0.0 0.0
  0.0 0.0 0.0 12.0 0.0
 15.0 13.0 15.0 0.0 0.0

```

Otherwise, what `Y[inds]` does is return a vector of all the values in `Y` that match your criterion. That is, `inds` is a vector of indices that match your criterion and then you get a vector out of `Y` of those corresponding indices. Does this make sense?

---

<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:** [February 19, 2022, 7:51pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771/8 "2022-02-19T19:51:03Z")

</div>

Guess I can just do a reshape and get what I want.

Many thanks All.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 19, 2022, 7:53pm UTC](https://discourse.julialang.org/t/index-of-a-grid/76771/9 "2022-02-19T19:53:57Z")

</div>

it’s about the intention being ambiguous. if your answer is it should return

```julia
[1 1
 1 0]

```

then why not

```julia
[1 0
 1 1]

```

if you know exactly what you will get, just do a reshape, but it’s not clear you can always get back a “matrix” without ambiguity
