Re: vector difference
- From: Gordon Sande <g.sande@xxxxxxxxxxxxxxxx>
- Date: Wed, 06 Aug 2008 19:15:25 GMT
On 2008-08-06 15:36:01 -0300, nospam@xxxxxxxxxxxxx (Richard Maine) said:
utab <umut.tabak@xxxxxxxxx> wrote:
real :: a(4) = (/1.,2.,3.,4./)
real :: b(1) = (/2., 3./)
Presumably you mean b(2), but that's just a typo.
result is to be
res = (/1., 4./)
I wondered if there is a quick way to accomplish this through intrinsic
functions?
I'd say that "pack" and "any" are your friends. I'd probably do
something like
logical :: mask(size(a))
integer :: i
real, allocatable:: res
...
do i = 1 , size(a)
mask(i) = any(a(i)==b)
end do
res = pack(a,mask)
This assumes the f2003 feature of allocate-on-assignment. For a
pre-f2003 compiler, you'd need to add an
allocate(res(count(mask))
Those who play with this kind of thing a lot could probably put all that
into a single statement, possibly using spread, plus some dim arguments.
I sort of think I can see how to do that, but I always have a hard time
reading that kind of stuff. As a consequence, I pretty much never write
it and I'm loath to try on the fly here. I'd do the multi-step version
above.
As well as being hard to read they have the disadvantage that the algorithms
being suggested are quadratic for a well/over researched problem that has
linear (and a bit) solutions. Perfectly acceptable for toy problems but
prone to create bottlenecks if the size grows at some later time. Having
the inputs sorted and using that fact lowers the cost markedly. Establishing
and maintaining a sorted order has it own costs.
This is the sort of thing that an algorithms course will go through. Some
of the AI languages have various of these algorithms built into their
supplied operations so can be both expressive and efficient on their
chosen problem domain.
As an aside, note that your problem definition seems to inherently be
based on exact comparisons of floatting point values. That is usually a
bad idea, but that is pretty much an independent question from this
array stuff. Change the arrays to, say, integer, and that objection
would go away.
.
- Follow-Ups:
- Re: vector difference
- From: glen herrmannsfeldt
- Re: vector difference
- From: utab
- Re: vector difference
- References:
- vector difference
- From: utab
- Re: vector difference
- From: Richard Maine
- vector difference
- Prev by Date: Re: vector difference
- Next by Date: Re: cray pointer declaration issue with gfortran
- Previous by thread: Re: vector difference
- Next by thread: Re: vector difference
- Index(es):
Relevant Pages
|