Scalar multiplication in R
I am trying to do a simple scalar multiplication in R, but I am facing some problem.
In linear algebra, I would do the following:
This is how I implemented it in R:
A <- matrix(1:4, 2, byrow = TRUE)
c <- matrix(rep(3, 4), 2)
A * c
This gives the correct conclusion, but creating a scalar matrix c would be cumbersome when it comes to larger matrices.
Is there a better way to do this?
+3
user4275591
source
to share
2 answers
The R
default is scalar. For matrix multiplication use %*%
. t
transposed and solve
will give you the opposite. Here are some examples:
a = matrix(1:4,2,2)
3 * a
c(1:2) %*% a
c(1:2) %*% t(a)
solve(a)
Here is the link: matrix algebra in R
+10
source to share