Fortran equivalent of numpy.where() function?

后端 未结 2 955
醉话见心
醉话见心 2020-12-21 11:14

I would like to do something like this in Fortran:

program where

real :: a(6) = (/ 4, 5, 6, 7, 8, 9 /)

print *, a(a>7)

end program

In

相关标签:
2条回答
  • 2020-12-21 11:36

    I'll extend slightly the answer by @VladimirF as I suspect you don't want to limit yourself to the exact print example.

    a>7 returns a logical array corresponding to a with .true. at index where the condition is met, .false. otherwise. The pack intrinsic takes such a mask and returns an array with those elements with .true. in the mask.

    However, you can do other things with the mask which may fit under your numpy.where desire. For example, there is the where construct (and where statement) and the merge intrinsic. Further you can use pack again with the mask to get the indices and do more involved manipulations.

    0 讨论(0)
  • 2020-12-21 11:39
    program where
    
    real :: a(6) = (/ 4, 5, 6, 7, 8, 9 /)
    
    print *, pack(a,a>7)
    
    end program
    
    0 讨论(0)
提交回复
热议问题