Re: [S] question

Franz-Josef Mueter (mueter@halibut.ims.uaf.edu)
Tue, 28 Apr 1998 11:39:10 -0800 (AKDT)


On Tue, 28 Apr 1998, Robert Lyles wrote:

> I have a question on a particular computation in Splus:
>
> Suppose "a" is a sequence of length n, and "b" is another sequence of
> length m, where m<n. You would like to return a sequence of length n,
> whose ith element (i=1,...,n) is the product over j=1,...,m of a function
> like Phi(a_i + a_i*b_j). Here, Phi is the standard normal cdf ("pnorm"),
> and i and j are being used to index the elements of "a" and "b",
> respectively.
>
> My question for more experienced Splus users is, is there an efficient way
> to do this, somehow taking advantage of the default matrix arithmetic
> rules (i.e., if possible without using a "for" loop, etc.)?

Here is a one-liner that should do what you want fairly efficiently:

> a <- rnorm(20)
> b <- rnorm(10)
> apply(matrix(a), 1, function(x) prod(pnorm(x+x*b)))
[1] 9.983299e-05 1.588974e-02 2.210214e-03 1.935949e-01 6.538284e-02
[6] 8.424254e-02 1.353865e-01 5.025441e-06 3.096180e-07 4.820503e-06
[11] 2.378148e-07 2.042163e-33 9.232172e-02 5.993221e-11 4.622130e-09
[16] 1.967170e-06 1.108977e-12 1.330225e-03 3.962734e-03 4.084612e-03
>

Franz

***************************************************************

"We learn geology the morning after the earthquake"

- Ralph Waldo Emerson

***************************************************************
Franz-Josef Mueter | Voice: (907) 474-7839
Institute of Marine Sciences | (907) 479-8815
School of Fisheries and Ocean Sciences | Fax: (907) 474-1943
University of Alaska | e-mail:
Fairbanks, Alaska, 99709-1080 | mueter@ims.alaska.edu
---------------------------------------------------------------
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news