Finding the intersect of two arrays in Fortran -


i'm trying generate intersect of 2 1-d arrays in fortran.

the intent use intersection mask in maxloc can pull max few elements 1 array (behavior analogous repeated root removal in max heap).

currently set value @ index found maxloc 0 after insert value @ index second array hoping fortran had clever mask-related method this. (it nice leave original array intact during procedure of max-retrieval)

i not quite sure mean. if want compare 2 arrays index index, can using ==, so:

integer :: a(4), b(4) logical :: inter(4)  = (/ 1, 2, 3, 4 /) b = (/ 4, 2, 3, 1 /) inter = (a == b)  ! (/ f, t, t, f /) 

this not technically intersection, mask in maxloc needs array of logical, assumed that want.

if want test whether value of a anywhere in b, have use @ least 1 do loop, think:

do j = 1, size(a)     inter(j) = any(a(j) == b) end 

if want find largest, say, n values, can use this:

function largest(vars, n)     implicit none     integer, intent(in) :: n     real, dimension(:), intent(in) :: vars     real, dimension(n) :: largest     integer ::     logical, dimension(size(vars)) :: m     integer :: mloc      m = .true.      = 1, n        mloc = maxloc(vars, dim=1, mask=m)        m(mloc) = .false.        largest(i) = vars(mloc)     end     return end function largest 

basically uses mask true, every time reverts mask highest false not value again in next iteration.

of course, of order (n*size(var)), if n large, might quicker bubble sort until have n largest values accumulated @ end, , pick them up.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -