Re: vector difference



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
.



Relevant Pages

  • Re: Array.which_long? ( I coded an extension for Array )
    ... Yeah I did not like this so I did not test it, ... yours of course I just used an Array instead of a Hash, ... res << s ...
    (comp.lang.ruby)
  • RE: How to compare hashes to find matching keys with conflicting values.
    ... I see that we are creating an array called res however I am not ... This would I think create an array based on the values contained in the ... register hash. ... I would have now way to link them back to the keys from ...
    (perl.beginners)
  • Re: dynamic array as return value?
    ... Such an array is known as a dynamic ... > return Res; ... When this function executes the 'return' statement, ... This is different to the strings of many other languages, ...
    (comp.lang.ada)
  • Re: awke vs. gawk
    ... which would set field $3 to the string "bob" while retaining all the original spacing. ... And I'd like to have the actual record separators as found in the input as an array, ... function selFields(fd, n, wanted, i, res) { ...
    (comp.lang.awk)
  • Re: vector difference
    ... I wondered if there is a quick way to accomplish this through intrinsic ... real, allocatable:: res ... This assumes the f2003 feature of allocate-on-assignment. ... array stuff. ...
    (comp.lang.fortran)