[R] Strange Matrix Multiplication Behaviour
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Oct 4 17:04:42 CEST 2004
This is *not* matrix multiplication, which uses %*% in R.
Might that help explain your confusion?
The difference is explained in `An Introduction to R'.
On Mon, 4 Oct 2004, Wayne Jones wrote:
>
> Hi there fellow R-users,
>
> Im seeing some strange behaviour when I multiply a vector by a matrix
>
> Here is my script:
>
> > tr
> 1 2 3 4 5 6
> 0.2217903 0.1560525 0.1487908 0.1671354 0.1590643 0.1471667
> >
> > ex1
> a b c d e f
> 1 0.2309579 -3.279045 -0.6694697 -1.1024404 0.2303928 -1.5527404
> 2 -0.2865357 -2.519789 -0.1138712 -0.3571832 -2.6727913 -0.3296945
>
> > is.vector(tr)
> [1] TRUE
>
> > is.data.frame(ex1)
> [1] TRUE
>
> > tr* ex1[1,]
> a b c d e f
> 1 0.05122423 -0.5117031 -0.09961092 -0.1842569 0.03664727 -0.2285117
>
> > (tr* ex1)[1,]
> a b c d e f
> 1 0.05122423 -0.4878917 -0.1064887 -0.2445106 0.03428033 -0.2469855
>
>
> Notice that the output from tr * ex[1,] is different from (tr* ex1)[1,]
> Especially element number 4.
>
> I would naturally expect both vectors to be equivalent.
Why? The second multiplies the elements of ex by tr, in column-major
order, and what is `natural' about that?
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list