next up previous contents
Next: Vector-valued Subscripts Up: Arrays Previous: Allocatable Arrays   Contents

Masked Assignment

Assignments can be executed for noregular sections of the arrays only. For example the assignments can be selectively executed only for those elements which satisfy a certain condition. This is accomplished using a WHERE construct.

Suppose for example we have three real matrices:


We want to change the matrix A such that ; if then we skip the division and let the old value of unchanged.

The form of the construct is

where (C /= 0)
  A = B/C
end where
The argument of WHERE is evaluated first. This argument is a matrix of logical elements, and is called the mask. The mask must conform to the implied shape of each assignment in the body. In our example


(all the elements of the mask are, conceptually, evaluated in parallel).

Next, the statements in the body of the WHERE construct are evaluated succesively. The matrix assignment C = A/B is performed element by element, but only for those elements for which .TRUE.; in our example, the result of the masked assignment is


The complete form of the WHERE construct includes an ELSEWHERE branch as well:

where (C /= 0)
  A = B/C
elsewhere
  A = -1.0  
end where
The ELSEWHERE assignments are performed for those elements for which .FALSE.; the result of our example is


Note: a short form is possible; similar to the IF statement, there is a WHERE statement which reads

where (C /= 0) A = B/C

WHERE statements cannot be nested. The execution first evaluates the mask (a matrix of .TRUE./.FALSE. depending on the elemental value of the condition) then executes the assignments for the .TRUE. positions (the WHERE block), then execute the assignments for the .FALSE. positions (the ELSEWHERE block).


next up previous contents
Next: Vector-valued Subscripts Up: Arrays Previous: Allocatable Arrays   Contents
Adrian Sandu 2001-08-26