Re: vector difference



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.


.



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: Array question
    ... res = Inputbox ... There are other ways, like in the initial redim, making an array larger than ... > array and then have the array display on a worksheet one element of the ...
    (microsoft.public.excel.programming)