2

I feel matrix operations in R is very confusing: we are mixing row and column vectors.

  • Here we define x1 as a vector, (I assume R default vector is a column vector? but it does not show it is arranged in that way.)

  • Then we define x2 is a transpose of x1, which the display also seems strange for me.

  • Finally, if we define x3 as a matrix the display seems better.

Now, my question is that, x1 and x2 are completely different things (one is transpose of another), but we have the same results here.

Any explanations? may be I should not mix vector and matrix operations together?

x1 = c(1:3) x2 = t(x1) x3 = matrix(c(1:3), ncol = 1) x1 [1] 1 2 3 x2 [,1] [,2] [,3] [1,] 1 2 3 x3 [,1] [1,] 1 [2,] 2 [3,] 3 x3 %*% x1 [,1] [,2] [,3] [1,] 1 2 3 [2,] 2 4 6 [3,] 3 6 9 x3 %*% x2 [,1] [,2] [,3] [1,] 1 2 3 [2,] 2 4 6 [3,] 3 6 9 
0

3 Answers 3

6

See ?`%*%`:

Description:

 Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix). 
Sign up to request clarification or add additional context in comments.

2 Comments

well, thanks for the information. I hope we can have some matlab style that just give me error instead of doing something automatically.
Note also: R specifically does not handle vectors as either a row or a column vector, which can easily be seen by noting that dim(x)==NULL and dim(t(x))==c(1,3). The misunderstanding arises if users assume that R defines row vectors because c() is seemingly defined "in a row." In a way, however, the R definition matches the mathematical definition, which also does not necessarily declare vectors as either row or column vectors aside from notational convention when working with matrices.
5

A numeric vector of length 3 is NOT a "column vector" in the sense that it does not have a dimension. However, it does get handled by %*% as though it were a matrix of dimension 1 x 3 since this succeeds:

 x <- 1:3 A <- matrix(1:12, 3) x %*% A #------------------ [,1] [,2] [,3] [,4] [1,] 14 32 50 68 #----also---- crossprod(x,A) # which is actually t(x) %*% A (surprisingly successful) [,1] [,2] [,3] [,4] [1,] 14 32 50 68 

And this does not:

 A %*% x #Error in A %*% x : non-conformable arguments 

Treating an atomic vector on the same footing as a matrix of dimension n x 1 matrix makes sense because R handles its matrix operations with column-major indexing. And R associativity rules proceed from left to right, so this also succeeds:

 y <- 1:4 x %*% A %*% y #-------------- [,1] [1,] 500 

Note that as.matrix obeys this rule:

> as.matrix(x) [,1] [1,] 1 [2,] 2 [3,] 3 

I think you should read the ?crossprod help page for a few more details and background.

Comments

1

Try the following

library(optimbase) x1 = c(1:3) x2 = transpose(x1) x2 [,1] [1,] 1 [2,] 2 [3,] 3 

As opposed to:

x2.t = t(x1) x2.t [,1] [,2] [,3] [1,] 1 2 3 

See documentation of transpose

transpose is a wrapper function around the t function, which tranposes matrices. Contrary to t, transpose processes vectors as if they were row matrices.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.