[R] Multrix-vector multiplication

MacQueen, Don macqueen1 at llnl.gov
Mon Aug 24 17:30:53 CEST 2015


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.



More information about the R-help mailing list