Hi all, I'm learning R. I'm used to Matlab and am having the following issue: I define a column vector and matrix and then multiply them. The result is not what I expect: v2 <- c(0,1,0) M <- cbind(c(1,4,7),c(2,5,8),c(3,6,9)) M*v2 [,1] [,2] [,3] [1,] 0 0 0 [2,] 4 5 6 [3,] 0 0 0 I expect the result to be a column, specifically the 2nd column of M. Now I want to left multiply the matrix by a row vector, and I get errors: t(v2)*M Error in t(v2) * M : non-conformable arrays> V2 <- t(v2) > V2*MError in V2 * M : non-conformable arrays I would expect to get the 2nd row of M as an output. Why am I not getting a column for the first case? Why is the second case causing errors? Any help would be greatly appreciated. I can get the -- View this message in context: http://r.789695.n4.nabble.com/row-matrix-tp3724023p3724023.html Sent from the R help mailing list archive at Nabble.com.
On Aug 6, 2011, at 4:35 PM, smajor wrote:> Hi all, I'm learning R. I'm used to Matlab and am having the > following issue: > > I define a column vector and matrix and then multiply them. The > result is > not what I expect: > v2 <- c(0,1,0) > M <- cbind(c(1,4,7),c(2,5,8),c(3,6,9)) > M*v2 > > [,1] [,2] [,3] > [1,] 0 0 0 > [2,] 4 5 6 > [3,] 0 0 0 > > I expect the result to be a column, specifically the 2nd column of M. > > Now I want to left multiply the matrix by a row vector, and I get > errors: > t(v2)*M > Error in t(v2) * M : non-conformable arrays >> V2 <- t(v2) >> V2*M > Error in V2 * M : non-conformable arrays > I would expect to get the 2nd row of M as an output. > > Why am I not getting a column for the first case? Why is the second > case > causing errors? Any help would be greatly appreciated. I can get theVector are not column or row vectors by default. They don't actually have a dim attribute unless you force one on them. Also you are using a scalar operator and need to use "%*%" to do matrix multiplication .... so: > dim(v2) <-c(1,3) > v2%*%M [,1] [,2] [,3] [1,] 4 5 6 As expected. So now you should go back to your introductory material as well as help("%*%") and help("*"). -- David Winsemius, MD West Hartford, CT
Hi, You are using the regular multiplication operator, when you want the matrix multiplication operator. Compare:> M * v2[,1] [,2] [,3] [1,] 0 0 0 [2,] 4 5 6 [3,] 0 0 0> M %*% v2[,1] [1,] 2 [2,] 5 [3,] 8 See ?"*" and ?"%*%" HTH, Josh On Sat, Aug 6, 2011 at 1:35 PM, smajor <semajor at ncsu.edu> wrote:> Hi all, I'm learning R. I'm used to Matlab and am having the following issue: > > I define a column vector and matrix and then multiply them. ?The result is > not what I expect: > v2 <- c(0,1,0) > M <- cbind(c(1,4,7),c(2,5,8),c(3,6,9)) > M*v2 > > ? ? [,1] [,2] [,3] > [1,] ? ?0 ? ?0 ? ?0 > [2,] ? ?4 ? ?5 ? ?6 > [3,] ? ?0 ? ?0 ? ?0 > > I expect the result to be a column, specifically the 2nd column of M. > > Now I want to left multiply the matrix by a row vector, and I get errors: > t(v2)*M > Error in t(v2) * M : non-conformable arrays >> V2 <- t(v2) >> V2*M > Error in V2 * M : non-conformable arrays > I would expect to get the 2nd row of M as an output. > > Why am I not getting a column for the first case? ?Why is the second case > causing errors? ?Any help would be greatly appreciated. ?I can get the > > -- > View this message in context: http://r.789695.n4.nabble.com/row-matrix-tp3724023p3724023.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, ATS Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
You need to learn that R syntax is not the same as Matlab syntax. If you want to use R, learn R. Read the basic introductory material, readily available from the R web site. In particular R treats multiplication --- i.e. "*" --- as multiplication. Arrays are multiplied entry by entry when they are conformable. When you multiply a vector times an array, the vector is recycled to get the requisite number of entries. The recycled entries are packed into an array of the appropriate dimension in reverse odometer order. What you want is matrix multiplication. In R this is denoted by "%*%". Note that M%*%v2 is the second column of M as you desired. HTH cheers, Rolf Turner On 07/08/11 08:35, smajor wrote:> Hi all, I'm learning R. I'm used to Matlab and am having the following issue: > > I define a column vector and matrix and then multiply them. The result is > not what I expect: >> M*v2 > > [,1] [,2] [,3] > [1,] 0 0 0 > [2,] 4 5 6 > [3,] 0 0 0 > > I expect the result to be a column, specifically the 2nd column of M. > > Now I want to left multiply the matrix by a row vector, and I get errors: > t(v2)*M > Error in t(v2) * M : non-conformable arrays >> V2<- t(v2) >> V2*M > Error in V2 * M : non-conformable arrays > I would expect to get the 2nd row of M as an output. > > Why am I not getting a column for the first case? Why is the second case > causing errors? Any help would be greatly appreciated. I can get the > > -- > View this message in context: http://r.789695.n4.nabble.com/row-matrix-tp3724023p3724023.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
By default, R operations on matrices, vectors, etc are elementwise like .* in Matlab. If you want matrix multiplication use %*% Hope this helps and good luck with R! Feel free to write back if I can help further. Michael Weylandt On Aug 6, 2011, at 4:35 PM, smajor <semajor at ncsu.edu> wrote:> Hi all, I'm learning R. I'm used to Matlab and am having the following issue: > > I define a column vector and matrix and then multiply them. The result is > not what I expect: > v2 <- c(0,1,0) > M <- cbind(c(1,4,7),c(2,5,8),c(3,6,9)) > M*v2 > > [,1] [,2] [,3] > [1,] 0 0 0 > [2,] 4 5 6 > [3,] 0 0 0 > > I expect the result to be a column, specifically the 2nd column of M. > > Now I want to left multiply the matrix by a row vector, and I get errors: > t(v2)*M > Error in t(v2) * M : non-conformable arrays >> V2 <- t(v2) >> V2*M > Error in V2 * M : non-conformable arrays > I would expect to get the 2nd row of M as an output. > > Why am I not getting a column for the first case? Why is the second case > causing errors? Any help would be greatly appreciated. I can get the > > -- > View this message in context: http://r.789695.n4.nabble.com/row-matrix-tp3724023p3724023.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.