apply mask from WHERE to a higher dimension array

Viewed 218

I want to apply the results from a WHERE construct to other arrays that have a higher dimension, so that in the following the "-" marks the indices from WHERE. I was wondering if FINDLOC could help, but seems it can not use a logical expression. Is there some kind of ARGWHERE intrinsic I am unaware of to return the indices and apply them ?

PROGRAM test
REAL :: a(3,20,20),b(20,20),c(20,20)
CALL random_number(c)
a=1
b=1
WHERE (c<0.5) 
  c=0
  b=0 ! this is fine
  a(:,-,-)=0 ! how to do this???
ENDWHERE
END PROGRAM test
1 Answers

For your b array, which has the same size as c, perhaps using MERGE would be more elegant:

b = MERGE(1,0,c>=0.5)

The same could be done with a, in a less elegant but still compact way:

FORALL(i=1:20,j=1:20) a(:,i,j) = merge(1,0,c(i,j)>=0.5)

Both these commands will provide an assigment to ALL elements in your arrays. If you want the filtering operation only, regardless of the initial values in the array, then perhaps they could be reduced to a WHERE construct:

FORALL(i=1:20,j=1:20,c(i,j)<0.5) a(:,i,j) = 0.0
WHERE (c<0.5) 
   b=0.0
   c=0.0
END WHERE

*** edit *** As per @HighPerformanceMark's suggestion, the latter example could be packed as

FORALL (i=1:20,j=1:20,c(i,j)<0.5)
   a(:,i,j) = 0.0
   b  (i,j) = 0.0
   c  (i,j) = 0.0
END FORALL

Related