Skip to content
Back to formatted view

Raw Message

Message-ID: <21F15D83-AB7C-4935-AE11-77B57B7D8A78@gmail.com>
Date: 2016-05-23T07:20:06Z
From: Peter Dalgaard
Subject: Element-by-element operation (adding)
In-Reply-To: <CA+hbrhUfhde0PfpNqzYUj-OYyKkbTzykz6THHDkFS09yXaAdng@mail.gmail.com>

> On 23 May 2016, at 07:44 , Peter Langfelder <peter.langfelder at gmail.com> wrote:
> 
> or
> 
> t(apply(v, 1, `+`, b))

Or, as you're messing with transposes anyways, use the fact that the column-wise counterpart is automagically handled by recycling:

t(t(v)+b)

Or, look Ma, no transposes
 
v + rep(b, each=nrow(v))

(_always_ doublecheck the logic when you apply these and similar techniques! I have seen my share of student code where recycling had been applied along the wrong dimension of a matrix...)

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com