Re: vector difference
- From: nospam@xxxxxxxxxxxxx (Richard Maine)
- Date: Wed, 6 Aug 2008 11:36:01 -0700
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 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.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.
- Follow-Ups:
- Re: vector difference
- From: Gordon Sande
- Re: vector difference
- From: utab
- Re: vector difference
- From: utab
- Re: vector difference
- References:
- vector difference
- From: utab
- vector difference
- Prev by Date: vector difference
- Next by Date: Re: vector difference
- Previous by thread: vector difference
- Next by thread: Re: vector difference
- Index(es):
Relevant Pages
|