# Equivalent of numpy.where in Fortran

**URL:** <https://fortran-lang.discourse.group/t/equivalent-of-numpy-where-in-fortran/6502>\
**Category:** Help\
**Created:** [September 13, 2023, 1:13am UTC](https://fortran-lang.discourse.group/t/equivalent-of-numpy-where-in-fortran/6502 "2023-09-13T01:13:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [September 13, 2023, 1:13am UTC](https://fortran-lang.discourse.group/t/equivalent-of-numpy-where-in-fortran/6502/1 "2023-09-13T01:13:43Z")

</div>

In python:

```auto
import numpy as np
a=np.array([0, 10, 2, 3, 4, 51, 6, 7, 8, 9])
print (np.where(a<=5)[0])
#output: array([0, 2, 3, 4])

```

Is there an equivalent of “where” in Fortran?

---

<div class="post-metadata">

**Author:** ![Chuckyvt](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@Chuckyvt](https://fortran-lang.discourse.group/u/Chuckyvt)\
**Post date:** [September 13, 2023, 1:28am UTC](https://fortran-lang.discourse.group/t/equivalent-of-numpy-where-in-fortran/6502/2 "2023-09-13T01:28:41Z")

</div>

PACK should work fine for this. A quick google search turned up this example, which should help you out.

> **[Fortran Tutorial =\> Using PACK to select elements meeting a condition](https://riptutorial.com/fortran/example/8789/using-pack-to-select-elements-meeting-a-condition)**
>
> Learn Fortran - Using PACK to select elements meeting a condition

---

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [September 13, 2023, 2:17am UTC](https://fortran-lang.discourse.group/t/equivalent-of-numpy-where-in-fortran/6502/3 "2023-09-13T02:17:00Z")

</div>

Thanks for pointing out this.

Here is the example:

```auto
module mod_mask
    implicit none

    contains
    
    subroutine where(arr, condition_scalar, indices)        
        real, intent(in) :: arr(:)
        real, intent(in):: condition_scalar
        integer, allocatable, intent(out) :: indices(:)
    
        integer:: i, n
        n = size(arr, dim=1)
        ! print*, "arr:", arr
        ! print*, "condition_scalar", condition_scalar
        indices = PACK([(i, i=1,n)], arr > condition_scalar)
        ! print*, "where:", indices
    end subroutine where

end module mod_mask

program maskk
    use mod_mask, only: where
    implicit none

    integer, allocatable :: indices(:)
    integer :: i !, n
    real, allocatable :: r(:)

    r = ([(i*1.1,i=10,15)])
    call where(r, 13., indices)
    print*, 'input array:', r
    print*, 'conditional scalar:', 13.
    print*, "indices:", indices
end program maskk
!
! input array: 11.0000000 12.1000004 13.2000008 14.3000002 15.4000006 !16.5000000    
! conditional scalar: 13.0000000    
! indices: 3 4 5 6
!

```

---

<div class="post-metadata">

**Author:** ![MarDie](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mardie/32/54_2.png) [@MarDie](https://fortran-lang.discourse.group/u/MarDie)\
**Post date:** [September 13, 2023, 6:06am UTC](https://fortran-lang.discourse.group/t/equivalent-of-numpy-where-in-fortran/6502/4 "2023-09-13T06:06:13Z")

</div>

According to the NumPy documentation ([numpy.where — NumPy v1.25 Manual](https://numpy.org/doc/stable/reference/generated/numpy.where.html)), NumPy’s `where` is meant for merging entries as in

```python
c = np.where(mask,a,b)

```

where `c` get values assigned from `a` and `b` depending on `mask`.

In Fortran, `merge` ([MERGE (The GNU Fortran Compiler)](https://gcc.gnu.org/onlinedocs/gfortran/MERGE.html)) or `where` ([https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-1/where.html](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-1/where.html)) can be used to achieve this.

---

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [September 13, 2023, 6:49am UTC](https://fortran-lang.discourse.group/t/equivalent-of-numpy-where-in-fortran/6502/5 "2023-09-13T06:49:30Z")

</div>

My aim is to look for the indices of the input array where index values is \<=5. Which numpy.where can do easily.

```auto
import numpy as np
a=np.array([0, 10, 2, 3, 4, 51, 6, 7, 8, 9])
print (np.where(a<=5)[0])
#output: array([0, 2, 3, 4])

```

And the same is done by the [pack function](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/pack-function.html) [as shown in the example above] in Fortran. It seems that merge may not do the same as it selects values from two arrays as shown here [merge function](https://gcc.gnu.org/onlinedocs/gfortran/MERGE.html).

---

<div class="post-metadata">

**Author:** ![MarDie](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mardie/32/54_2.png) [@MarDie](https://fortran-lang.discourse.group/u/MarDie)\
**Post date:** [September 13, 2023, 8:10am UTC](https://fortran-lang.discourse.group/t/equivalent-of-numpy-where-in-fortran/6502/6 "2023-09-13T08:10:35Z")

</div>

yes, but the pythonic way of doing this would be

```python
import numpy as np
a=np.array([0, 10, 2, 3, 4, 51, 6, 7, 8, 9])
print (np.asarray(a<=5).nonzero()[0])
#output: [0, 2, 3, 4]

```

so if people search for the equivalent of `np.where`, `merge` or `where` are relevant Fortran functions.
