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]]
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.
I would suggest you look into the Matrix package. Here's an example that illustrates what I suspect is the root cause of your issue:> x <- matrix(1:4, ncol=2) > class(x)[1] "matrix"> y1 <- x[,1] > class(y1)[1] "integer"> y1[1] 1 2> y2 <- x[,1,drop=FALSE] > class(y2)[1] "matrix"> y2[,1] [1,] 1 [2,] 2> as.matrix(y1)[,1] [1,] 1 [2,] 2 If you assign your objects the "Matrix" class as defined in that package, then perhaps your 'a' will be a matrix after your step 1. (I have not tried to verify this) -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 8/22/15, 6:43 AM, "R-help on behalf of Steven Yen" <r-help-bounces at r-project.org on behalf of 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.