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:

scalar multiplication

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


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


Use a function drop()

to convert a 1x1 variable matrix to a "real" scalar. So you can write drop(c)*A

and you don't need to replace c

with the value itself.



+1


source







All Articles