# What is a mask? What is its syntax?

**URL:** <https://fortran-lang.discourse.group/t/what-is-a-mask-what-is-its-syntax/5712>\
**Category:** Help\
**Created:** [May 1, 2023, 2:59pm UTC](https://fortran-lang.discourse.group/t/what-is-a-mask-what-is-its-syntax/5712 "2023-05-01T14:59:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamyakes](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@adamyakes](https://fortran-lang.discourse.group/u/adamyakes)\
**Post date:** [May 1, 2023, 2:59pm UTC](https://fortran-lang.discourse.group/t/what-is-a-mask-what-is-its-syntax/5712/1 "2023-05-01T14:59:34Z")

</div>

Ultimate newbie question here. I keep seeing `MASK` as an argument name in intrinsics, and some examples, but I for the life of me cannot find a standalone explanation of how masks work. The closest I get for [googling](https://www.google.com/search?hl=en&q=fortran%20mask%20explanation) it is this [IBM](https://www.ibm.com/docs/en/xl-fortran-linux/15.1.3?topic=construct-interpreting-masked-array-assignments) page, which redirects to “Product version no longer published”. I apologize if this has been asked before - I’ve tried to look for this directly.

I understand it’s an expression involving arrays, that’s about it.

For a little context, I have a large `if` statement like `if (i == SOME_CONSTANT .or. i == SOME_OTHER_CONSTANT)` (there’s about 20 or so checks). I’ve heard of the `any` intrinsic and I think the code could be replaced with `if (any(my_array_of_constants))` but I don’t know how to write the expression I want.

---

<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:** [May 1, 2023, 3:46pm UTC](https://fortran-lang.discourse.group/t/what-is-a-mask-what-is-its-syntax/5712/2 "2023-05-01T15:46:37Z")

</div>

Here is an example:

```auto
implicit none

real:: a(5), b
a=[real:: 1, 2.3, 3.4, 6.0, 0.0]

print*, 'array a:', a

b = minval(a, mask= a > 0 )
print*, b ! ans=1; if mask in not there then minval is 0. 

b = sum(a, mask= a > 1 )
print*, b ! ans: 11.70; sum of 2.3, 3.4, 6.0 
end

```

output:

```auto
gfortran -o test_mask.x test_mask.f90 && ./test_mask.x
 array a: 1.00000000 2.29999995 3.40000010 6.00000000 0.00000000
   1.00000000
   11.6999998

```

bests!

---

<div class="post-metadata">

**Author:** ![adamyakes](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@adamyakes](https://fortran-lang.discourse.group/u/adamyakes)\
**Post date:** [May 1, 2023, 3:53pm UTC](https://fortran-lang.discourse.group/t/what-is-a-mask-what-is-its-syntax/5712/3 "2023-05-01T15:53:22Z")

</div>

I appreciate the examples. I have some clarifying questions:

What exactly does the expression `a > 0` produce? Does it produce `[.true., .true., .true., .true., .false.]`? Is this something fancy with function application like `elemental` functions, or is it just generic syntax that is convenient to use here? In what ways can you mix scalars and arrays? What about having two arrays?

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [May 1, 2023, 4:22pm UTC](https://fortran-lang.discourse.group/t/what-is-a-mask-what-is-its-syntax/5712/4 "2023-05-01T16:22:09Z")

</div>

Yes, it produces what you wrote. You can use comparison operators between

1. 
  - two scalars, producing a scalar

2. 
  - between an array and a scalar, giving a logical array of the same shape as the array

3. 
  - or between two arrays of the same shape, giving a logical array of their common shape.

---

<div class="post-metadata">

**Author:** ![adamyakes](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@adamyakes](https://fortran-lang.discourse.group/u/adamyakes)\
**Post date:** [May 1, 2023, 4:24pm UTC](https://fortran-lang.discourse.group/t/what-is-a-mask-what-is-its-syntax/5712/5 "2023-05-01T16:24:04Z")

</div>

So a `mask` isn’t anything fancy, just a common parameter name. Thanks!

---

<div class="post-metadata">

**Author:** ![Patrick](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/patrick/32/3170_2.png) [@Patrick](https://fortran-lang.discourse.group/u/Patrick)\
**Post date:** [May 2, 2023, 5:55pm UTC](https://fortran-lang.discourse.group/t/what-is-a-mask-what-is-its-syntax/5712/6 "2023-05-02T17:55:00Z")

</div>

Yu wrote: but I for the life of me cannot find a standalone explanation of how masks work.

Fortran 90/95 Programming Manual (upc.edu)  
[https://www-eio.upc.edu/lceio/manuals/Fortran95-manual.pdf](https://www-eio.upc.edu/lceio/manuals/Fortran95-manual.pdf)

---

<div class="post-metadata">

**Author:** ![adamyakes](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@adamyakes](https://fortran-lang.discourse.group/u/adamyakes)\
**Post date:** [May 2, 2023, 6:11pm UTC](https://fortran-lang.discourse.group/t/what-is-a-mask-what-is-its-syntax/5712/7 "2023-05-02T18:11:43Z")

</div>

Thanks for the reply. I think the issue is not that no sources explain masks, it’s that none of them _call_ array operations masks, which confused me when I would read about “the mask”. I’d also read information like

> The elemental function **merge** (3) selects values from two arrays or scalars according to a logical mask.

And assume that “a logical mask” was a “thing” in Fortran. A first-class concept. But since it’s really nothing special, a “standalone” expression makes no sense. This link, for example, does not contain the word mask (insofar as Ctrl + F can detect).
