Vectorized array comparison in Fortran

余生颓废 提交于 2019-12-13 07:47:16

问题


I would like to perform the do-end do bit of the following pseudocode within Fortran using a single line statement:

integer, parameter :: N = 1000
integer, dimension(1:N) :: ArrayA, ArrayB
logical, dimension(1:N) :: ArrayL
...
...
do i = 1, N
    if( ArrayA(i) <= ArrayB(i) ) then
        ArrayL(i) = .true.
    else
        ArrayL(i) = .false.
    end if
end do

Is this possible? If so, how do I do so?


回答1:


integer, parameter :: N = 1000
integer, dimension(1:N) :: ArrayA, ArrayB
logical, dimension(1:N) :: ArrayL
...
...
ArrayL = (ArrayA <= ArrayB)



回答2:


@Mchinoune answer is perfectly good. I find the WHERE/ELSEWHERE to be more readable, and if you are doing later operations then the MASK is handy, and one visually can identify that as a MASK.

integer, parameter      :: N = 1000
integer, dimension(1:N) :: ArrayA, ArrayB
logical, dimension(1:N) :: The_Mask_Where_A_lt_B = .FALSE.
integer                 :: Some_MinVal
...
...
WHERE(ArrayA <= ArrayB) The_Mask_Where_A_lt_B = .TRUE.

!I know that this is not part of the question...
Some_MinVal = MINVAL(ArrayA, MASK=The_Mask_Where_A_lt_B)


来源:https://stackoverflow.com/questions/44075566/vectorized-array-comparison-in-fortran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!