The FORALL Statement
The FORALL statement allows for more common assignments to sections of an array. A FORALL statement has the general form.
FORALL ( triplet, ..., triplet, mask )
where triplet has the general form
subscript = lower: upper : step-size
and states a set of indices. step-size is optional. Statement is an pointer or arithmetic assignment and the assignment statement is evaluated for those index values shown by the list of triplets that are not rejected by the optional mask.
Example 7 The following statements set each element of matrix X to the sum of its indices.
FORALL (i=1:m, j=1:n) X(i,j) = i+j
and the following statement sets the upper right triangle of matrix Y to zero .
FORALL (i=1:n, j=1:n, i
Multi-statement FORALL construct:
Multi-statement FORALL is shorthand for the series of single statement FORALLs. The
syntax for FORALL is
FORALL (index-spec-list [,mask]) Body
Nesting of FORALL is allowed.
Let a= [2,4,6,8,10], b=[1,3,5,7,9], c=[0,0,0,0,0]
Consider the following program segment
FORALL (i = 2:4) a(i) = a(i-1)+a(i+1) c(i) = b(i) *a(i+1). END FORALL
The computation will be a =a+a =2+6=8 a =a+a =4+8=12 a =a+a = 6+10=16
c = b *a = 3*12=36 c = b *a = 5*16=80
c = b *a =7*10=70
Thus output is
a= [2,8,12,16,10], b=[1,3,5,7,9], c=[0,36,80,70,0]