algorithm - Efficient way to see if a set of values appear in an array? -


i'm trying check if 1d array of integers a contains or not, @ every 1 of it's size(a) positions, of elements of set of integers s (also 1d array), general case of size(s) > 1.

the easy , obvious way following nested loop:

do = 1, size(a)    j = 1, size(s)         if(a(i) == s(j)) ** **    enddo  enddo 

the problem that, large arrays a , s, process inefficient. there intrinsic fortran subroutine or function faster? or other method?

i've tried following, doesn't want compile:

do = 1, nnodes    if(a(i) == any(s)) ** ** enddo 

the error message appears following: "error #6362: data types of argument(s) invalid." i'm using vs2010 intel parallel studio 2013.

the expression

a(i) == any(s) 

has integer on lhs , logical on rhs. we'll have none of c-inspired nonsense of regarding comparable types in fortran thank much. actually, it's worse that, any returns logical takes array of logicals on input, any(array_of_int) won't compile.

you try

any(s==a(i)) 

instead. should give compilable solution.

now, efficiency, you're first snippet o(n^2). can better, asymptotically. sort both arrays , scan them in tandem, o(n + n log n) or similar. if need coding up, update question, though suspect it's been asked , answered here on so.

i suspect, , can check if care to, using any inside single (explicit) loop o(n^2) -- since any has operate on general cases can't see realistic alternative scanning array -- loop in other words.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -