hxd1011 - 11 months ago 36

R Question

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

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

- Then we define is a transpose of
`x2`

, which the display also seems strange for me.`x1`

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

Now, my question is that,

`x1`

`x2`

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

Answer

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).`

Source (Stackoverflow)