Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

matvec

Multiply a Matrix by a Vector


Description

Multiply the rows or columns of a matrix by the elements of a vector.

Usage

matvec(M, v)
vecmat(v, M)

Arguments

M

numeric matrix, or object which can be coerced to a matrix.

v

numeric vector, or object which can be coerced to a vector. Length should match the number of columns of M (for matvec) or the number of rows of M (for vecmat)

Details

matvec(M,v) is equivalent to M %*% diag(v) but is faster to execute. Similarly vecmat(v,M) is equivalent to diag(v) %*% M but is faster to execute.

Value

A matrix of the same dimensions as M.

Author(s)

Gordon Smyth

Examples

A <- matrix(1:12,3,4)
A
matvec(A,c(1,2,3,4))
vecmat(c(1,2,3),A)

statmod

Statistical Modeling

v1.4.36
GPL-2 | GPL-3
Authors
Gordon Smyth [cre, aut], Yifang Hu [ctb], Peter Dunn [ctb], Belinda Phipson [ctb], Yunshun Chen [ctb]
Initial release
2021-05-10

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.