# [R] Matrix element-by-element multiplication

David Winsemius dwinsemius at comcast.net
Sun Nov 6 15:24:12 CET 2011

```On Nov 6, 2011, at 12:21 AM, R. Michael Weylandt 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.

Because the dimensions did not work. dim(v)[1] (the rows) did not
equal dim(b)[2] (the columns) since b did not have a dimension.

>> Then,
>>
>>    dp <- c(v)*b
>>
>> worked.

It worked because of argument recycling. It did not give you a matrix
result, however, because of what Michael said. c() turns a matrix into
a vector, which it was all along anyway.

Here's an example of argument recycling:
> c(1, 2, 3) * 1:12
[1]  1  4  9  4 10 18  7 16 27 10 22 36

The 1,2,3 vector gets implicitly lengthened as would have happened
with rep(c(1,2,3), 4) and then

--
David.

>>
>> 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
>>>
>>> --

David Winsemius, MD
West Hartford, CT

```