Re: Need efficient routine to calculate max of two arrays [message #3402] |
Wed, 25 January 1995 17:07  |
mfishler
Messages: 5 Registered: October 1994
|
Junior Member |
|
|
In article <3g3p8i$1o6@cobra.aer.com>, gallery@aer.com (William O. Gallery) writes:
> Here is the problem:
>
> There are two arrays: a and b of equal length, I want to calculate
> an array c of the same length so that:
> c(i) = min(a(i),b(i))
>
> For efficiency, I want to use vectors and not a do loop.
>
> Any suggestions? This would seem to be a common enough problem,
>
For the operation stated, c = a < b will do the trick.
For max instead of min, use c = a > b .
Good luck!
==Matthew
--
+---------+----------------------------------------+-------- --------------+
| (_|_ | M a t t h e w G. F i s h l e r | |
| ___ | The Johns Hopkins School of Medicine | "It is a miracle |
| (_) ) | 720 Rutland Ave --- 701 Traylor Bldg | that curiosity |
| _ | Baltimore, MD 21205 | survives formal |
| (_ | tel:(410)955-0077 fax:(410)955-0549 | education." |
| (_ | e-mail: mfishler@bme.jhu.edu | --Albert Einstein |
+---------+----------------------------------------+-------- --------------+
|
|
|
Re: Need efficient routine to calculate max of two arrays [message #3410 is a reply to message #3402] |
Thu, 26 January 1995 06:25  |
pa64
Messages: 3 Registered: January 1995
|
Junior Member |
|
|
In article <3g3p8i$1o6@cobra.aer.com>, gallery@aer.com (William O. Gallery) writes:
|> Here is the problem:
|>
|> There are two arrays: a and b of equal length, I want to calculate
|> an array c of the same length so that:
|> c(i) = min(a(i),b(i))
|>
|> For efficiency, I want to use vectors and not a do loop.
|>
|> Any suggestions? This would seem to be a common enough problem,
|>
|>
|>
|> --
|> William O. Gallery gallery@aer.com
|> Atmospheric & Environmental Research Voice(617) 349-2284
|> 840 Memorial Drive (617) 547-6207
|> Cambridge, Massachusetts 02139 FAX (617) 661-6479
Up to four arrays, the following function seems to be quite efficient:
function pmin,im1,im2,im3,im4
if n_params() gt 3 then begin
index = where(im4 lt im3, count)
if count gt 0 then im3(index) = im4(index)
endif
if n_params() gt 2 then begin
index = where(im3 lt im2, count)
if count gt 0 then im2(index) = im3(index)
endif
if n_params() gt 1 then begin
index = where(im2 lt im1, count)
if count gt 0 then im1(index) = im2(index)
endif
return,im1
end
Regards,
Hermann Mannstein
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
Hermann Mannstein Tel.: +49 8153 28-2503
Institut fuer Physik der Atmosphaere or -1118
DLR - Oberpfaffenhofen Fax.: +49 8153 28-1841
Postfach 1116 email.: H.Mannstein@dlr.de
D-82230 Wessling \
Germany \ O
________\/|________
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`--------\--------'~ ~~~~~~~~~~~~~~~~~~~~
\
|
|
|
|
|
Re: Need efficient routine to calculate max of two arrays [message #3426 is a reply to message #3420] |
Tue, 24 January 1995 23:55  |
karsten
Messages: 2 Registered: January 1995
|
Junior Member |
|
|
In article <3g3p8i$1o6@cobra.aer.com>, gallery@aer.com (William O. Gallery) writes:
|> Here is the problem:
|>
|> There are two arrays: a and b of equal length, I want to calculate
|> an array c of the same length so that:
|> c(i) = min(a(i),b(i))
|>
|> For efficiency, I want to use vectors and not a do loop.
|>
|> Any suggestions? This would seem to be a common enough problem,
|>
|>
|>
|> --
|> William O. Gallery gallery@aer.com
|> Atmospheric & Environmental Research Voice(617) 349-2284
|> 840 Memorial Drive (617) 547-6207
|> Cambridge, Massachusetts 02139 FAX (617) 661-6479
Try,
c = a < b
----
Karsten Heia
Applied physics
Univesity of Tromsoe
Tromsoe
NORWAY
|
|
|
Re: Need efficient routine to calculate max of two arrays [message #3427 is a reply to message #3426] |
Tue, 24 January 1995 18:45  |
kommers
Messages: 3 Registered: January 1995
|
Junior Member |
|
|
> In article <3g3p8i$1o6@cobra.aer.com>, gallery@aer.com (William O. Gallery) writes:
> |> Here is the problem:
> |>
> |> There are two arrays: a and b of equal length, I want to calculate
> |> an array c of the same length so that:
> |> c(i) = min(a(i),b(i))
I think the < and > operators implement this functionality efficiently.
For example:
IDL> a = [5,4,3,2,1]
IDL> b = [1,2,3,4,5]
IDL> c = a < b
IDL> print, c
1 2 3 2 1
The < operator does the "min" part, and > does the "max" part.
For more info, see under "operators" in the User's Guide.
Cheers,
Jeff
|
|
|
RE: Need efficient routine to calculate max of two arrays [message #3429 is a reply to message #3427] |
Tue, 24 January 1995 17:29  |
mallozzi
Messages: 60 Registered: August 1994
|
Member |
|
|
|> There are two arrays: a and b of equal length, I want to calculate
|> an array c of the same length so that:
|> c(i) = min(a(i),b(i))
Try this:
IDL> a = [1., 2, 3, 4, 5]
IDL> b = [1., 2, 3, 2, 1]
IDL> c = a < b
IDL> print, a
1.00000 2.00000 3.00000 4.00000 5.00000
IDL> print, b
1.00000 2.00000 3.00000 2.00000 1.00000
IDL> print, c
1.00000 2.00000 3.00000 2.00000 1.00000
mallozzi@ssl.msfc.nasa.gov
|
|
|
Re: Need efficient routine to calculate max of two arrays [message #3430 is a reply to message #3429] |
Tue, 24 January 1995 15:12  |
dan
Messages: 27 Registered: March 1993
|
Junior Member |
|
|
In article <3g3p8i$1o6@cobra.aer.com>, gallery@aer.com (William O. Gallery) writes:
|> Here is the problem:
|>
|> There are two arrays: a and b of equal length, I want to calculate
|> an array c of the same length so that:
|> c(i) = min(a(i),b(i))
|>
|> For efficiency, I want to use vectors and not a do loop.
|>
|> Any suggestions? This would seem to be a common enough problem,
|>
|>
|>
|> --
|> William O. Gallery gallery@aer.com
|> Atmospheric & Environmental Research Voice(617) 349-2284
|> 840 Memorial Drive (617) 547-6207
|> Cambridge, Massachusetts 02139 FAX (617) 661-6479
How about
c = a
c(where(b lt a)) = b(where(b lt a))
--
************************************************************ ***
** Dan Bergmann dbergmann@llnl.gov **
** Global Climate Research fax (510) 422-5844 **
** Lawrence Livermore National Lab human (510) 423-6765 **
|
|
|