[R] Matrix element-by-element multiplication
Joshua Wiley
jwiley.psych at gmail.com
Mon Nov 7 02:01:03 CET 2011
Hi,
R may not have a special "scalar", but it is common, if informal, in
linear algebra to refer to a 1 x 1 matrix as a scalar. Indeed,
something like:
1:10 * matrix(2)
or
matrix(2) * 1:10
are both valid. Even
matrix(2) %*% 1:10
and
1:10 %*% matrix(2)
work, where the vector seems to be silently coerced to a matrix. R
even seems to work hard to convert to a conformable matrix:
## works:
1:10 %*% matrix(1:10)
## does not work
matrix(1:10) %*% matrix(1:10)
## works
t(matrix(1:10)) %*% matrix(1:10)
Interestingly, there is actually a (rather old) comment in arithmetic.c
/* If either x or y is a matrix with length 1 and the other is a
vector, we want to coerce the matrix to be a vector.
Do we want to? We don't do it! BDR 2004-03-06
*/
Given the coersion that already occurs with vectors to matrices for
%*% and matrices to vectors for *, it seems not unreasonable to
convert a 1 x 1 matrix to a vector _for_ * so that the following
yields identical results:
matrix(1:9, 3) * matrix(2)
matrix(1:9, 3) * 2
Of course in the mean time, or in general, it is a good habit to
create or explicity coerce objects yourself rather than relying on R
to make smart guesses about what should be happening.
Cheers,
Josh
On Sun, Nov 6, 2011 at 4:02 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> <michael.weylandt at gmail.com> wrote:
> It looks like pdf is not a "scalar" (that term actually has no meaning in R but I know what you mean) but is rather a 1x1 matrix, as attested by the fact it has dimensions. If you give dnorm() a matrix it will return one, as it did here.
>
> Perhaps you should look at the is.matrix() and as.vector() functions rather than abusing a side-effect of c(), which makes it much more difficult to see R's internal logic, which, while quirky, is useful at the end of the day.
>
> Michael
>
> PS - It's good form to cc the list at each step so others can follow along and contribute when I say something wrong. It also helps you get quicker answers.
>
> On Nov 6, 2011, at 1:06 AM, Steven Yen <syen at utk.edu> wrote:
>
>> I am trying to multiply what I know is a scalar (pdf(xb)) to a column vector of coefficient (bb).
>> In the following, pdf is a scalar and bb is 5 x 1. I first show what worked and then what did not work.
>> If my pdf is a scalar, why would I need c(pdf) to be able to pre-multiply it by a 5 x 1 vector?
>>
>> ---
>>
>> > x <- as.matrix(colMeans(x))
>> > xb <- t(x)%*%bb
>> > pdf <- dnorm(xb)
>>
>> > dim(bb)
>> [1] 5 1
>>
>> >
>> > cpdf <- c(pdf)
>> > dim(cpdf)
>> NULL
>> > cpdf
>> [1] 0.304201
>> > (dphat <- cpdf*bb)
>> [,1]
>> (Intercept) 0.32744753
>> xrage -0.00599225
>> xryr 0.01758431
>> xrrate -0.08217250
>> xrrel -0.05695434
>> >
>> > pdf <- dnorm(xb)
>> > dim(pdf)
>> [1] 1 1
>> > pdf
>> [,1]
>> [1,] 0.304201
>> > (dphat <- pdf*bb)
>> Error in pdf * bb : non-conformable arrays
>> >
>>
>> At 12:21 AM 11/6/2011, you wrote:
>>> There are a few (nasty?) side-effects to c(), one of which is
>>> stripping a matrix of its dimensionality. E.g.,
>>>
>>> x <- matrix(1:4, 2)
>>> c(x)
>>> [1] 1 2 3 4
>>>
>>> So that's probably what happened to you. R has a somewhat odd feature
>>> of not really considering a pure vector as a column or row vector but
>>> being willing to change it to either:
>>>
>>> e.g.
>>>
>>> y <- 1:2
>>>
>>> x %*% y
>>> y %*% x
>>> y %*% y
>>>
>>> while matrix(y) %*% x throws an error, which can also trip folks up.
>>> You might also note that x * y and y*x return the same thing in this
>>> problem.
>>>
>>> Getting back to your problem: what are v and b and what are you hoping
>>> to get done? Specifically, what happened when you tried v*b (give the
>>> exact error message). It seems likely that they are non-conformable
>>> matrices, but here non-conformable for element-wise multiplication
>>> doesn't mean the same thing as it does for matrix multiplication.
>>> E.g.,
>>>
>>> x <- matrix(1:4,2)
>>> y <- matrix(1:6,2)
>>>
>>> dim(x)
>>> [1] 2 2
>>>
>>> dim(y)
>>> [1] 2 3
>>>
>>> x * y -- here R seems to want matrices with identical dimensions, but
>>> i can't promise that.
>>>
>>> x %*% y does work.
>>>
>>> Hope this helps and yes I know it can seem crazy at first, but there
>>> really is reason behind it at the end of the tunnel,
>>>
>>> Michael
>>>
>>>
>>> On Sun, Nov 6, 2011 at 12:11 AM, Steven Yen <syen at utk.edu> wrote:
>>> > My earlier attempt
>>> >
>>> > dp <- v*b
>>> >
>>> > did not work. Then,
>>> >
>>> > dp <- c(v)*b
>>> >
>>> > worked.
>>> >
>>> > Confused,
>>> >
>>> > Steven
>>> >
>>> > At 09:10 PM 11/4/2011, you wrote:
>>> >
>>> > Did you even try?
>>> >
>>> > a <- 1:3
>>> > x <- matrix(c(1,2,3,2,4,6,3,6,9),3)
>>> > a*x
>>> >
>>> > [,1] [,2] [,3]
>>> > [1,] 1 2 3
>>> > [2,] 4 8 12
>>> > [3,] 9 18 27
>>> >
>>> > Michael
>>> >
>>> > On Fri, Nov 4, 2011 at 7:26 PM, Steven Yen <syen at utk.edu> wrote:
>>> >> is there a way to do element-by-element multiplication as in Gauss
>>> >> and MATLAB, as shown below? Thanks.
>>> >>
>>> >> ---
>>> >> a
>>> >>
>>> >> 1.0000000
>>> >> 2.0000000
>>> >> 3.0000000
>>> >> x
>>> >>
>>> >> 1.0000000 2.0000000 3.0000000
>>> >> 2.0000000 4.0000000 6.0000000
>>> >> 3.0000000 6.0000000 9.0000000
>>> >> a.*x
>>> >>
>>> >> 1.0000000 2.0000000 3.0000000
>>> >> 4.0000000 8.0000000 12.000000
>>> >> 9.0000000 18.000000 27.000000
>>> >>
>>> >>
>>> >> --
>>> >> Steven T. Yen, Professor of Agricultural Economics
>>> >> The University of Tennessee
>>> >> http://web.utk.edu/~syen/
>>> >> [[alternative HTML version deleted]]
>>> >>
>>> >> ______________________________________________
>>> >> R-help at r-project.org mailing list
>>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>>> >> PLEASE do read the posting guide
>>> >> http://www.R-project.org/posting-guide.html
>>> >> and provide commented, minimal, self-contained, reproducible code.
>>> >>
>>> >
>>> > --
>>> > Steven T. Yen, Professor of Agricultural Economics
>>> > The University of Tennessee
>>> > http://web.utk.edu/~syen/
>> --
>> Steven T. Yen, Professor of Agricultural Economics
>> The University of Tennessee
>> http://web.utk.edu/~syen/
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/
More information about the R-help
mailing list