将 WHERE 中的掩码应用于更高维度的数组 [英] apply mask from WHERE to a higher dimension array

查看:52
本文介绍了将 WHERE 中的掩码应用于更高维度的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 WHERE 构造的结果应用于具有更高维度的其他数组,以便在下面的-"标记来自 WHERE 的索引.我想知道 FINDLOC 是否可以提供帮助,但似乎它不能使用逻辑表达式.是否有某种 ARGWHERE 内在我不知道返回索引并应用它们?

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

推荐答案

对于你的 b 数组,它与 c 的大小相同,也许使用 MERGE 会更优雅:

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)

同样可以用 a 来完成,以一种不太优雅但仍然紧凑的方式:

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)

这两个命令都将为数组中的所有元素提供分配.如果您只想要过滤操作,而不管数组中的初始值如何,那么也许可以将它们简化为 WHERE 构造:

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

*** 编辑 ***根据@HighPerformanceMark 的建议,后一个示例可以打包为

*** 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

这篇关于将 WHERE 中的掩码应用于更高维度的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆