# How to subset an array using a logical mask?

**URL:** <https://fortran-lang.discourse.group/t/how-to-subset-an-array-using-a-logical-mask/3645>\
**Category:** Homework\
**Created:** [June 2, 2022, 6:35pm UTC](https://fortran-lang.discourse.group/t/how-to-subset-an-array-using-a-logical-mask/3645 "2022-06-02T18:35:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorvz](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jorvz](https://fortran-lang.discourse.group/u/jorvz)\
**Post date:** [June 2, 2022, 6:35pm UTC](https://fortran-lang.discourse.group/t/how-to-subset-an-array-using-a-logical-mask/3645/1 "2022-06-02T18:35:30Z")

</div>

Hey friends,

Lets say i have a grid of cells and a array called ‘cell’ that, for example, cell(4) = 1 and cell(8)=1, that means that the particle 4 and particle 8 are in cell number 1, so, i need a way to know that these two particles are in these cell, for example, in a loop passing through all the cells and i’m in c=1 so i need that an array particles\_in\_cell = (4,8). The particle 4 have an x coordinate x(4) and particle 8 x(8), x is an array that have the coordinates of all N particles, but i want x\_c to be just the coordinates of the particles that are in that cell c. In python thats is easy to be x\_c = x[particles\_in\_cell] that makes me the array of the coordinates of just the two particles but in fortran i have no idea on how to make that.

edit1: i was thinking in something like x\_c = x(cell == c) but cell==c it’s a logical array so… yeah idk

---

<div class="post-metadata">

**Author:** ![milancurcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/milancurcic/32/2_2.png) [@milancurcic](https://fortran-lang.discourse.group/u/milancurcic)\
**Post date:** [June 2, 2022, 7:27pm UTC](https://fortran-lang.discourse.group/t/how-to-subset-an-array-using-a-logical-mask/3645/2 "2022-06-02T19:27:43Z")

</div>

Would the intrinsic [pack](https://gcc.gnu.org/onlinedocs/gfortran/PACK.html) work?

```auto
x_c = pack(x, cell == c)

```

You could also do it with the standard library’s [trueloc](https://stdlib.fortran-lang.org/proc/trueloc.html):

```auto
x_c = x(trueloc(cell == c))

```

---

<div class="post-metadata">

**Author:** ![jorvz](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jorvz](https://fortran-lang.discourse.group/u/jorvz)\
**Post date:** [June 2, 2022, 7:46pm UTC](https://fortran-lang.discourse.group/t/how-to-subset-an-array-using-a-logical-mask/3645/3 "2022-06-02T19:46:04Z")

</div>

i think it is perfect! thank you i didn’t know this pack function

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [June 3, 2022, 10:20am UTC](https://fortran-lang.discourse.group/t/how-to-subset-an-array-using-a-logical-mask/3645/4 "2022-06-03T10:20:49Z")

</div>

Should you need it: It is accompanied by the unpack function that expands the array.
