Re: [S] Q: subtracting a constant from each col of a matrix

Christian Keller (ckeller@aicos.com)
Wed, 18 Mar 1998 18:03:39 +0100


Joe Davis wrote:
> ..., I often want to subtract a different constant from each col.
> of a matrix ( the vector of constants being a nominal value of
> some sort and the matrix being the predictions of some sort).
>
> a <- c(1,2,3)
> X <- cbind(c(1,1,1),c(2,2,2),c(3,3,3))
> ...
> My question is "is there a better way to do this?"
> ...

Yes. Short and easy: (Note that FUN="-" is the default)
sweep(X, 2, a, FUN="-")

-- Christian
----------------------------------------------------------
Christian Keller Tel: +41 61 686 98 81
AICOS Technologies AG Fax: +41 61 686 98 88
Efringerstrasse 32 email: ckeller@aicos.com
CH-4057 Basel, Switzerland Web: http://www.aicos.com/
----------------------------------------------------------
-----------------------------------------------------------------------
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