问题
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