[R] Multrix-vector multiplication

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sat Aug 22 16:30:49 CEST 2015


The actual types of data you have are critical to understanding your problem, and you have not provided that information. [1] What looks like a matrix isn't always, and in R vectors do not have a "column" or "row" nature, so matrix multiplication is not necessarily well-defined. To get consistent results, make sure you actually are working with matrices rather than vectors, data frames or other data types. The str function is your friend.

Please clarify what data you are actually working with using the dput as described in the link [1]. Also use plain text, as the HTML formatting in your email usually corrupts what you think you see by the time we see it.

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On August 22, 2015 6:43:54 AM PDT, Steven Yen <syen04 at gmail.com> wrote:
>I had trouble with matrix multiplication when a matrix reduces to a
>vector.  In the following, lines 1 and 2 work when matrices u and a are
>both of order 2.
>Lines 3 and 5 do not work (message is matrix not conformable) when u is
>(T
>x 1) and a is (1 x 2) and This causes a problem for users of other
>matrix
>languages such as Gauss and MATLAB.
>Inserting line 4 makes it work, which is annoying. But, is it
>proper/safe
>to make it work by inserting line 4? Other approaches?
>Thank you!
>
>1 a<-solve(s22)%*%s21 # (2 x 2) <- (2 x 2) %*% (2 x 2)
>2 uc<-u%*%v$a         # (T x 2) <- (T x 2) %*% (2 x 2)
>
>3 a<-solve(s22)%*%s21 # (1 x 2) <- (1 x 1) %*% (1 x 2)
>4 a<-as.matrix(a)     # This line makes it work. OK? Other approaches?
>5 uc<-u%*%v$a         # (T x 1) %*% (1 x 2)
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.



More information about the R-help mailing list